0

I have the following structure:

public class A{}

public Class B:A
{
    public virtual C { get; set;}
}

public Class C{}

public Class Context:DbContext
{
    public DbSet<A> As { get; set; }
    public DbSet<C> Cs { get; set; }
}

When I want to load any object b, the property C of this object is null. In the database the column C has an id in the table for A. Other int or string properties can be loaded without a problem.

using(var ctx = new Context())
{
    B b = ctx.As.FirstOrDefault() as B;
}

Can I solve this problem using lazy loading? Using Eagerly Loading I have a problem to include the property B, because I have a list List<A> someAs where some of the items are from type B.

I tried to keep this example as simple as possible. Just tell me to provide more information.

jasdefer
  • 757
  • 12
  • 24

2 Answers2

1

If you want to eagerly load a property of a subclass, you must explicitly query the subclass:

ctx.As.OfType<B>().Include(b => b.Cs).FirstOrDefault();
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Ah perfect. I think this will do exactly what I want. I am going to try this tomorrow and tell you if it works. One question: why does lazy loading not work in this case? – jasdefer Dec 07 '14 at 21:05
  • Oh, lazy loading should work too if you cast an `A` to a `B`, but IMO eager loading is the preferred approach. – Gert Arnold Dec 07 '14 at 21:09
  • It works. Thank you very much. I couldn't get it to work using lazy loading. I don't want to use it but I am curious. How should I cast `A` to `B`. I tried: `A a = ctx.As.FirstOrDefault();`and then `B b = (B)a` but that did not work. – jasdefer Dec 08 '14 at 06:19
0

You can continue using lazy loading and specify to load child relations by using the .Include(). Example:

using (var context = new BloggingContext()) 
{ 
    // Load all blogs and related posts 
    var blogs1 = context.Blogs 
                        .Include(b => b.Posts) 
                        .ToList(); 

    // Load one blogs and its related posts 
    var blog1 = context.Blogs 
                        .Where(b => b.Name == "ADO.NET Blog") 
                        .Include(b => b.Posts) 
                        .FirstOrDefault(); 

    // Load all blogs and related posts  
    // using a string to specify the relationship 
    var blogs2 = context.Blogs 
                        .Include("Posts") 
                        .ToList(); 

    // Load one blog and its related posts  
    // using a string to specify the relationship 
    var blog2 = context.Blogs 
                       .Where(b => b.Name == "ADO.NET Blog") 
                       .Include("Posts") 
                       .FirstOrDefault(); 
} 

Take a look at this post: http://msdn.microsoft.com/en-us/data/jj574232.aspx

rodrigogq
  • 1,943
  • 1
  • 16
  • 25
  • Thank you for the answer. I cannot use `ctx.As.Include(x => x.C)` because it is not a property of `A`. I have the problem with the inheritance, not with Eagerly Loading in general. Or did I miss something? – jasdefer Dec 07 '14 at 19:21