实体框架的重复性问题

发布于 2024-10-26 18:09:33 字数 986 浏览 1 评论 0原文

我正在使用 EF 4.1 CTP5 和 SQL Server 2008。我需要了解如何解决重复性问题。我有以下 2 个类:

public class Nation   
{
    public int ID {get; set;}
    public string name {get;set;}
    public List<NationAlly> NationAllies {get;set;}
}

public class NationAlly
{
    public int ID {get; set;}
    public int level {get;set;}
    public Nation Owner {get; set;}
    public Nation toNation {get;set;}
}

实体映射到带有两个表(Nations 和 NationAllies)的数据库。除此之外,还有两种关系。 1)从NationAllies.OwnerID到Nation.ID 2)从 NationAllies.ToNationID 到 Nation.ID

当我尝试从数据库中检索 Nation 类型的对象时,我访问 DbContext 类 NationDB:

Nation selectedNation = ((nationDB.Nations.Include("NationAllies")).Where(m => m.name == "France")).FirstOrDefault();

问题是我得到了一个 selectedNation 对象,其中包含 NationAllies 列表,但每个 NationAlly 都在该列表具有字段 toNation = null。 首先,我希望 toNation 字段从数据库中检索正确的信息。我该怎么做?

当然,一个国家将与其他国家盟友连接,而这些国家又将拥有另一个国家。如何构建递归映射?我的想法是通过以特定方式查询数据库来导航地图直到某个级别。那么,获得良好速度性能的最佳方法是什么?

I'm working with EF 4.1 CTP5 and SQL Server 2008. I need to understand how to solve a recurrency problem. I have the following 2 classes:

public class Nation   
{
    public int ID {get; set;}
    public string name {get;set;}
    public List<NationAlly> NationAllies {get;set;}
}

public class NationAlly
{
    public int ID {get; set;}
    public int level {get;set;}
    public Nation Owner {get; set;}
    public Nation toNation {get;set;}
}

The entities are mapped to the database with two tables (Nations and NationAllies). Besides, there are two relationships. 1) From NationAllies.OwnerID to Nation.ID
2) From NationAllies.ToNationID to Nation.ID

When I try to retrieve an object of Nation type from my database, I access the DbContext class NationDB:

Nation selectedNation = ((nationDB.Nations.Include("NationAllies")).Where(m => m.name == "France")).FirstOrDefault();

The problem is that I get a selectedNation object which has a list of NationAllies but every NationAlly in the list has the field toNation = null.
First of all I would like the field toNation to retrieve the correct information from the database. How do I do this?

Then of course toNation will be connected with other NationAllies which on their turn will have an other Nation. How could possibly a recursive map be built? My idea is to navigate the map until a certain level, by querying the database in a specific way. Doing so, what would be the best approach to have good speed performance?

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

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

发布评论

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

评论(1

神经大条 2024-11-02 18:09:33

看起来 NationAllies 是具有附加属性的联结表。问题是,如果您没有在 Include 方法中显式指定嵌套导航属性,EF 不会立即加载它们。如果您想要填充 toNation,则必须使用

nationDB.Nations.Include("NationAllies.toNation")

或者

nationDB.Nations.Include(n => n.NationAllies.Select(na => na.toNation))

您也可以启用延迟加载。将所有导航属性设为虚拟toNationNationOwnerNationAllies),除非您关闭上下文,否则所有属性都会一旦您第一次访问它们就会被加载。

It looks like NationAllies is junction table with additional properties. The problem is that EF doesn't eager load nested navigation properties if you do not specify them explicitly in Include method. If you want to have toNation filled you must use

nationDB.Nations.Include("NationAllies.toNation")

or

nationDB.Nations.Include(n => n.NationAllies.Select(na => na.toNation))

You can also enable lazy loading. Make all your navigation properties virtual (toNation, NationOwner and NationAllies) and unless you close the context all properties will be loaded once you first access them.

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