简体   繁体   English

EF Code First:包括不处理可选关系

[英]EF Code First: Include not working on optional relationship

I have a specific query in my code which needs to eager load all related entities (both ->1 FKs and ->N FKs) because the context will be disposed right after that.我的代码中有一个特定的查询,它需要预先加载所有相关实体(->1 FK 和 ->N FK),因为上下文将在此之后立即处理。

I made a generic "Query" method that takes params Expression<Func<MyItem, object>>[] includes and then internally chains them.我制作了一个通用的“查询”方法,该方法采用params Expression<Func<MyItem, object>>[] includes然后在内部链接它们。 That part works fine.那部分工作正常。

The query looks like this:查询如下所示:

var item = facade.Query<MyItem>(
                c => c.Childs.Select(x => x.Parent),
                c => c.Childs.Select(x => x.SubChild1),
                c => c.Childs.Select(x => x.SubChildNotWorking),
                c => c.Childs.Select(x => x.SubChild2),
                c => c.Childs.Select(x => x.SubChild3),
                c => c.Childs.Select(x => x.SubChildrens)
                ).FirstOrDefault(c => c.Name == name);

The mapping for the not working property (placed in the configuration of SubChildNotWorking):不工作属性的映射(放置在 SubChildNotWorking 的配置中):

this.HasMany(scnw => scnw.Childs).WithOptional(c => c.SubChildNotWorking).HasForeignKey(c => c.MyForeignKey);

Of all the includes, only the SubChildNotWorking doesn't actually work.在所有包含中,只有SubChildNotWorking实际上不起作用。 When inspecting with the debugger the returned object, I see the proxies on all the properties.当使用调试器检查返回的 object 时,我看到了所有属性的代理。 Opening the proxies gives me the correct data for all other relations, and a "The objectcontext has already been disposed exception" for the SubChildNotWorking property.打开代理为我提供了所有其他关系的正确数据,以及SubChildNotWorking属性的“对象上下文已被处置异常”。

The only difference I was able to spot is that the SubChildNotWorking is a nullable FK (with nullable column on the DB and WithOptional configuration in the dbcontext) while all the others are non nullable FKs configured with WithRequired.我能够发现的唯一区别是SubChildNotWorking是一个可为 null 的 FK(在 DB 上具有可为 null 的列,在 dbcontext 中具有 WithOptional 配置),而所有其他都是使用 WithRequired 配置的不可为 null 的 FK。

The database also is a legacy DB not created with Code First and not following its conventions, I just made the mappings in the DbContext.该数据库也是一个遗留数据库,不是使用 Code First 创建的,也没有遵循其约定,我只是在 DbContext 中进行了映射。 Everything else works fine.其他一切正常。

I am trying to figure out if eager loading doesn't work on nullable FKs but I couldn't find any documentation about that.我试图弄清楚预加载是否不适用于可为空的 FK,但我找不到任何相关文档。

Is that a bug, or intended behavior?这是一个错误,还是预期的行为? But most of all, how do I solve this?但最重要的是,我该如何解决这个问题?

Thanks.谢谢。

Did you define你定义了吗

[DataContract]

on your class (entities)?在您的 class(实体)上? If so, don't forget to annotate your SubChild with如果是这样,不要忘记用

[DataMember] 

or it might end up loaded but not showing up when you perform your GET call on entities.或者当您对实体执行 GET 调用时,它可能最终加载但未显示。

When you are working with the include method, you are talking about the eager load pattern.当您使用 include 方法时,您就是在谈论急切加载模式。 Entities with relationship will be loaded as collections. For any not-nullable foreign key, there's a trivial collection starting empty, but for nullable ones, there is not.具有关系的实体将加载为 collections。对于任何不可为空的外键,都有一个从空开始的普通集合,但对于可为空的外键,则没有。 On the Code First you could make the navigation property virtual, but it's not your case.在 Code First 上,您可以将导航属性设为虚拟,但这不是您的情况。 You can try load it directly on Context, for example:你可以尝试直接在 Context 上加载它,例如:

var someEntity = context.someEntities.Find(1);
context.Entry(someEntity ).Reference(e => e.EntityWithFKNullable).Load(); 

or the shorter version:或更短的版本:

context.EntitiesWithFKNullable.Load();

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

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