I noticed that in some case, all fields of the entity are not preloaded : only the fields of the most basic type are preloaded, and the other fields are lazyloaded on demand, this will cause a sql query by field.

How can I preload all fields? In this particular I will enumerate all fields by reflection.

Sample model

  [Serializable]
  [HierarchyRoot]
  public class MyEntity : Entity
  {
    [Field, Key]
    public int Id { get; private set; }
  }

  public class MyEntityExtended : MyEntity
  {
    [Field(Length = 100)]
    public string Text1 { get; set; }

    [Field(Length = 100)]
    public string Text2 { get; set; }

    [Field(Length = 100)]
    public string Text3 { get; set; }
  }

Sample code

  Key key = null;

  using (var session = domain.OpenSession())
  using (var t = session.OpenTransaction())
  {
    session.Activate();
    MyEntityExtended helloWorld = new MyEntityExtended(session);
    key = Key.Create<MyEntity>(domain, helloWorld.Id);
    t.Complete();
  }

  using (var session = domain.OpenSession())
  using (var t = session.OpenTransaction())
  {
    Entity entity = session.Query.SingleOrDefault(key);
    foreach (FieldInfo field in entity.TypeInfo.Fields)
    {
      object value = entity.GetProperty<object>(field.Name);
    }

    t.Complete();
  }

SQL Executed in second session

SELECT [a].[Id], [a].[TypeId] FROM [dbo].[MyEntity] [a] WHERE ([a].[Id] IN (@p1_0_0_0));
SELECT [a].[Id], [a].[TypeId], [a].[Text1] FROM [dbo].[MyEntityExtended] [a] WHERE ([a].[Id] IN (@p1_0_0_0));
SELECT [a].[Id], [a].[TypeId], [a].[Text2] FROM [dbo].[MyEntityExtended] [a] WHERE ([a].[Id] IN (@p1_0_0_0));
SELECT [a].[Id], [a].[TypeId], [a].[Text3] FROM [dbo].[MyEntityExtended] [a] WHERE ([a].[Id] IN (@p1_0_0_0));

asked Jul 26 '12 at 08:46

olorin's gravatar image

olorin
358848792

edited Jul 26 '12 at 09:41


One Answer:

Hello olorin,

Likely you've found a bug in prefetching logic.

In case you posted DataObjects.Net doesn't know the exact type of Entity to fetch, so it fetches the columns that present in MyEntity type (it gets this knowledge from key) and this is expected behavior.

However, when later you access any field that is not fetched yet, prefetch layer should find all fields that are empty and fetch them in a single query.

To be fixed.

answered Jul 26 '12 at 09:12

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

Thank you. FYI I tested with version 4.4.2.8482

(Jul 26 '12 at 09:41) olorin olorin's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×573

Asked: Jul 26 '12 at 08:46

Seen: 3,087 times

Last updated: Jul 26 '12 at 09:41

powered by OSQA