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

  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())
    MyEntityExtended helloWorld = new MyEntityExtended(session);
    key = Key.Create<MyEntity>(domain, helloWorld.Id);

  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);


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


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

Thank you. FYI I tested with version

(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

powered by OSQA