简体   繁体   English

EF 4.1 抽象集合加载问题

[英]EF 4.1 abstract collection loading problem

I have a problem regarding EF 4.1 code first.我首先遇到了关于 EF 4.1 代码的问题。 I am trying to demonstrate with a simplified example.我试图用一个简化的例子来演示。

Let's say we have a Country class which contains a States collection.假设我们有一个包含 States 集合的 Country class。 State collection contains schools collection. State 收藏包含学校收藏。 School is an abstract class.学校是一个抽象的class。 It has specializations of ElementarySchool and HighSchool.它有小学和高中的专业。

HighSchool has a collection property of DrivingCourses. HighSchool 具有 DrivingCourses 的集合属性。 DrivingCourses and all other data saves into the db successfully.驾驶课程和所有其他数据成功保存到数据库中。

My problem is when I'm loading the Country class the DrivingCourses collection remains null.我的问题是当我加载国家 class 时,DrivingCourses 集合仍然是 null。 (everything else is ok) (其他一切正常)

As I understand the problem is because when ef loads and populates the HighSchool class it's not aware of the courses collection.据我了解,问题是因为当 ef 加载并填充 HighSchool class 时,它不知道课程集合。

I am unable to add this mapping because with the fluent api's static reflecion I can only map properties of the (abstract) School class.我无法添加此映射,因为使用 fluent api 的 static 反射我只能 map (抽象)学校 class 的属性。

I'm using the default config for abstraction: Table per Hierarchy Could someone please brighten me, if it's possible to solve my problem with EF 4.1?我正在使用默认配置进行抽象:每个层次结构的表如果可以解决我的 EF 4.1 问题,有人可以给我亮一下吗?

Thanks in advance, Sandor提前致谢, 桑多

If I understand your description correctly then your model looks roughly like this (I omit key properties and so on):如果我正确理解了您的描述,那么您的 model 大致如下(我省略了关键属性等):

public class Country
{
    public ICollection<State> States { get; set; }
}

public class State
{
    public ICollection<School> Schools { get; set; }
}

public abstract class School { ...  }

public class ElementarySchool : School { ... }

public class HighSchool : School
{
    public ICollection<DrivingCourse> DrivingCourses { get; set; }
}

public class DrivingCourse { ... }

And you have a DbContext which includes public DbSet<Country> Countries { get; set; }你有一个 DbContext ,其中包括public DbSet<Country> Countries { get; set; } public DbSet<Country> Countries { get; set; } public DbSet<Country> Countries { get; set; } . public DbSet<Country> Countries { get; set; }

Now you want to load all Countries (or a filtered collection of Countries) including all navigation properties (especially also the DrivingCourses ).现在您要加载所有Countries (或国家/地区的过滤集合),包括所有导航属性(尤其是DrivingCourses )。

I don't know if this is possible with a single roundtrip to the database (by eager loading all collections).我不知道这是否可以通过单次往返数据库(通过急切加载所有集合)来实现。 A solution which will require multiple roundtrips though might be this one:一个需要多次往返的解决方案可能是这个:

// Load all Countries including `States` and `Schools` collection
// but not the `DrivingCourses` collection
var countryList = context.Countries
    .Include(c => c.States.Select(s => s.Schools))
    .ToList();

// Create in-memory list of all loaded Schools of type HighSchool
IEnumerable<HighSchool> highSchoolList =
    countryList.SelectMany(c =>
                  c.States.SelectMany(s => s.Schools.OfType<HighSchool>()));

// Explicitely load the DrivingCourses one by one
foreach (var item in highSchoolList)
    context.Entry(item).Collection(h => h.DrivingCourses).Load();

Just as a first idea.就像第一个想法一样。 It's likely that there are better solutions.可能有更好的解决方案。

Edit编辑

Using Load on the Countries DbSet doesn't change the problem.在 Country DbSet 上使用Load不会改变问题。 Load is the same as ToList() without actually returning a result, the entities are just loaded into the context. LoadToList()相同,没有实际返回结果,实体只是加载到上下文中。 The code above could be rewritten like so:上面的代码可以这样重写:

context.Countries.Include(c => c.States.Select(s => s.Schools)).Load();

IEnumerable<HighSchool> highSchoolList =
    context.Countries.Local.SelectMany(c =>
                  c.States.SelectMany(s => s.Schools.OfType<HighSchool>()));

foreach (var item in highSchoolList)
    context.Entry(item).Collection(h => h.DrivingCourses).Load();

But this is basically the same as before and it also doesn't solve the problem to load the DrivingCourses in the first Load statement in a single DB roundtrip.但这与以前基本相同,并且它也没有解决在单个 DB 往返中的第一个Load语句中加载DrivingCourses的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM