为什么RelationshipManager.GetRelatedReference(,)总是返回EntityReference?具有空值?

发布于 2024-10-28 00:49:46 字数 473 浏览 0 评论 0原文

由于某种原因,导航属性不适用于我的实体框架模型。

从 N->1 方向,每次我尝试获取 EntityReference 时,它​​都会带有 null 值,即使 EntityKey 是正确的。

从方向1->N,集合始终为空。

这种行为在我的整个模型中是一致的。

无论原因是什么,我认为它应该引发异常,而不是默默地检索不一致的引用。

引用带有空值的可能原因是什么?

编辑

我刚刚注意到它与延迟加载有关。 EntityReference(T) 的 IsLoaded 属性设置为 false,并且显式调用 Load 方法可以解决该问题。问题在于访问导航属性时调用的方法 RelationshipManager.GetRelatedReference 应加载 EntityReference。不应该吗?

For some reason, navigation properties are not working on my Entity Framework model.

From the direction N->1, every time I attempt to obtain a EntityReference it comes with null value, even though the EntityKey is correct.

From the direction 1->N, the collection is always empty.

This behavior is consistent throughout my entire model.

Whatever the reason is, I think it should raise an exception intead of silently retrieving an inconsistent reference.

What are the possible reasons a reference would come with null value?

EDIT

I just noticed it has something to do with lazy loading. The EntityReference(T) is coming with the IsLoaded property set to false and the explicit call of the Load method solves the problem. The problem is that the method RelationshipManager.GetRelatedReference that is called when the navigation property is accessed should load the EntityReference. Should'nt it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

度的依靠╰つ 2024-11-04 00:49:46

此问题是由 LazyLoadingEnabled 属性的含义引起的。

乍一看,LazyLoadingEnabled = false 似乎意味着 EF 在加载对象时会加载对象关系,当然有一些限制以防止 EF 加载整个数据库。实际上,这意味着关系将永远被隐式加载。也就是说:从N->1方向,返回的EntityReference(T)将有一个正确的EntityKey,但是IsLoaded< /code> 将为 false,Value 将为 null。另一方面,在方向 1->N 中,集合将为空,IsLoaded 将为 false。可以使用 Load 方法显式加载 EntityReferenceEntityCollection

LazyLoadingEnabled = true,另一方面,意味着它似乎意味着。这些关联将在需要时加载。

默认值是 false,这顺便触发了我经历的所有这些混乱。

为了防止混淆,也许应该有一个名为 LoadingMode 的属性,它是一个具有有意义值的枚举。例如:懒惰渴望

This problem is because of the meaning of the LazyLoadingEnabled property.

At first glance, LazyLoadingEnabled = false seems to mean that EF will load the object relationships when the object is loaded, of course with some limitations to prevent EF to load the entire database. Actually, it means that the relationships will never be implitly loaded. That is: from the direction N->1, the returned EntityReference(T) will have a correct EntityKey but IsLoaded will be false and Value will be null. In the other hand, in the direction 1->N, the collection will be empty, IsLoaded will be false. Either the EntityReference or the EntityCollection can be explicitly loaded using the Load method.

LazyLoadingEnabled = true, in the other hand, means that it seems to mean. The associations will be loaded as they are needed.

The default is false, which by the way triggered all this confusion I went through.

To prevent confusions, maybe there should be a property called LoadingMode which would be an enum with meaningful values. Like: None, Lazy, Eager

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文