I use EF 6.1 and have two classes as DB model:
public class A {
public int Id { get; set; }
// ...
public virtual List<B> Bs { get; set; }
}
public class B {
public int Id { get; set; }
public MyType Type { get; set; }
public byte[] Data { get; set; }
public int AId { get; set; }
[ForeignKey("AId")]
public virtual A A { get; set; }
}
enum MyType { x1, x2 }
How could load A entitys and their related entity B which has B.Type == x1
and use them in disconnected mode.
I want something like this (doesn't exist)
context.A.Include(a => a.Bs.Where(a => a.Type == x1))
this query load all related entity
context.A.Include(a => a.Bs)
I've tried :
context.Entry(a).Collection(a => a.Bs).Query()..Where(b => b.Type == x1).Load();
but after closing context, access to B
throws exception.
any suggestion?
Edit: I also tried load each entity in separate query
var AQuery = context.A.Where ...
var aList = AQuery.ToList();
var bList = (from b in context.B
where b.Type == x1 && AQuery.Contains(b.A)
select b).ToList();
but I can't manually add b
to A.Bs
list (because Virtual property I guess)