gpt4 book ai didi

entity-framework - Entity Framework DB First : Convert Associative Table to Navigation Properties

转载 作者:行者123 更新时间:2023-12-03 17:52:59 25 4
gpt4 key购买 nike

我首先使用实体​​框架数据库,但我想从代码优先范例中复制以下行为:

在 Entity Framework Code First 中,您可以执行以下操作:

public class Thing
{
public int ID { get; set; }
ICollection<Stuff> Stuffs { get; set; }
}

public class Stuff
{
public int ID { get; set; }
ICollection<Thing> Things { get; set; }
}

并且数据库会生成和关联表来表示多对多的关系。

我在旧数据库中使用 Database First。我拉入了实体,其中包含表示两个表之间多对多关系的关联表。

由于关联表作为实体包含在内,因此导航属性如下:
public class Thing
{
public int ID { get; set; }
public ICollection<ThingStuff> ThingStuffs { get; set; }
}

public class ThingStuff
{
public int ThingID { get; set; }
public int StuffID { get; set; }
ICollection<Thing> Things { get; set; }
ICollection<Stuff> Stuffs { get; set; }
}

public class Stuff
{
public int ID { get; set; }
public ICollection<ThingStuff> ThingStuffs { get; set; }
}

所以要导航,我必须:
var stuff = Thing.ThingStuffs.Select(ts => ts.Stuff);

代替:
var stuff = Thing.Stuffs;

所以问题是:

有什么方法可以删除表示关联的实体 (ThingStuff) 并告诉 EntityFramework 关于现有表以创建多对多导航属性?

最佳答案

如 fluent api 文档中所述,映射复合键不是更好吗? http://msdn.microsoft.com/en-us/data/jj591617.aspx

关于entity-framework - Entity Framework DB First : Convert Associative Table to Navigation Properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16552544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com