C# 实体框架打乱了我的多对多自我关系
我有一个Products表和BundleLink表,其中定义了哪个产品是另一个产品的捆绑产品C# 实体框架打乱了我的多对多自我关系,c#,sql,sql-server,entity-framework,C#,Sql,Sql Server,Entity Framework,我有一个Products表和BundleLink表,其中定义了哪个产品是另一个产品的捆绑产品 public class Product { public int Id { get; set; } public ICollection<BundleLink> MasterOf { get; set; } public ICollection<BundleLink> BundleOf { get; set;
public class Product
{
public int Id { get; set; }
public ICollection<BundleLink> MasterOf { get; set; }
public ICollection<BundleLink> BundleOf { get; set; }
}
public class BundleLink
{
public int Id { get; set; }
public Product Master { get; set; }
public Product Bundle { get; set; }
public int Quantity { get; set; }
}
公共类产品
{
公共int Id{get;set;}
{get;set;}的公共ICollection master
{get;set;}的公共ICollection捆绑包
}
公共类BundleLink
{
公共int Id{get;set;}
公共产品主程序{get;set;}
公共产品包{get;set;}
公共整数数量{get;set;}
}
因此,当我去看一个主产品时,我会看到哪些捆绑包指向它,或者指向另一个方向
但是实体框架在BundleLink表的sql server中创建了4列。它为我定义的产品字段创建了一个外键,并创建了另外两个外键,其中一个外键每次都为null,这取决于我添加bundle的方向。我如何为EF定义哪个BundeLink系列属于哪个产品
我希望我的描述可以理解。
提前感谢。在实体框架中,您需要遵循EF 6和EF Core中的数据注释-ForeignKey属性 ForeignKey属性用于在EF 6和EF Core中的两个实体之间的关系中配置外键。它覆盖默认约定。根据默认约定,EF在属性名称与相关实体的主键属性匹配时将其作为外键属性 更多信息: 谢谢,
raghu在实体框架中,您需要遵循EF 6和EF Core中的数据注释-ForeignKey属性 ForeignKey属性用于在EF 6和EF Core中的两个实体之间的关系中配置外键。它覆盖默认约定。根据默认约定,EF在属性名称与相关实体的主键属性匹配时将其作为外键属性 更多信息: 谢谢, 拉古 我如何为EF定义哪个BundeLink系列属于哪个产品 您有两种解决方案:
- 在
的DbConext
方法中,使用流畅的配置,通过定义关系,它可以帮助EF知道哪些集合属于哪些集合OnModelCreating
- 用
属性装饰您的收藏。因此,在您的收藏中,您将为InverseProperty
收藏提供MasterOf
,为inverseProperty(“Master”)
提供BundleOf
inverseProperty(“Bundle”)
- 在
的DbConext
方法中,使用流畅的配置,通过定义关系,它可以帮助EF知道哪些集合属于哪些集合OnModelCreating
- 用
属性装饰您的收藏。因此,在您的收藏中,您将为InverseProperty
收藏提供MasterOf
,为inverseProperty(“Master”)
提供BundleOf
inverseProperty(“Bundle”)