如何确定是否导航属性在实体框架设置,而无需加载相关记录 [英] How to determine if Navigation Property in the Entity Framework is set without loading the related records

查看:319
本文介绍了如何确定是否导航属性在实体框架设置,而无需加载相关记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道导航性能的EF 4所以我恳请大家一个交代

I'm not sure about Navigational Properties in EF 4 so I would kindly ask you an explanation.

让我们想象一下这样的情景:

Lets imagine this scenarios:

A)

我有两个实体 A 有关系N至N(多对多)和的表在我的数据库
A B 和一个链接表 AB 有两个外键

I have two Entities A and B with a relation N to N (many to many) and tree Table in my Data Base A and B and a Link Table AB with two Foreign Key.

在这种情况下EF创建一个导航属性可以称之为 X ,也是一个 X基准

In this scenario EF create a Navigational Property lets call it X and also a XReference.

b)

我有两个实体 A b 有关系1至N(一对多)和的两个的表在我的数据库
A b 有一个外键

I have two Entities A and B with a relation 1 to N (one to many) and two Table in my Data Base A and B with one Foreign Key.

在这种情况下EF创建一个导航属性可以称之为 而不是 YReference < 。/ STRONG>

In this scenario EF create a Navigational Property lets call it Y but not a YReference.

现在让我们方案A和b,并试图找出是否有在b A的任何引用:

Now lets take Scenario A and B and try to find out if there is any reference of A in B:

我对场景代码:

A):

bool isA = a.XReference.EntityKey != null;

我不负载B记录(正确吗?)

b):

bool isA = a.B.Any(x => x.BId == AId);

我做负载B记录

我的问题:


  • 为什么EF不会创建一个YReference,我不能使用的EntityKey 在方案b属性。

  • 在我的代码情景b,我真的不从b加载任何记录?

  • 你知道一个更好的方法来快速运行此查询?

  • Why EF does not create a YReference and I cannot use EntityKey property in Scenario B.
  • In my Code Scenario B, Do I really do not load any records from B?
  • Do you know a better approach to run this query fast?

谢谢你们的帮助,我希望我能够说清楚: - )

Thanks guys for your help, I hope I was able to make it clear :-)

推荐答案

下面是检查是否为实体相关的记录被加载与否的方式

Here is a way to check if related records for an entity is loaded or not.

有关实体,你必须与你可以检查像下面的实体的多个记录。(一对多的关系)

For entity where you have multiple records related to entity you can check like below.(One to many relationship)

myDbContext.Entry(MyEntity).Collection(x => x.NavigationalProperyName).IsLoaded

和如果只涉及到实体中的一个记录,那么你可以检查像下面。(一对一的关系)

And If have only one record related to entity, then you can check like below.(One to one relationship)

myDbContext.Entry(MyEntity).Reference(x => x.NavigationalProperyName).IsLoaded

这篇关于如何确定是否导航属性在实体框架设置,而无需加载相关记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆