Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架5代码优先自引用多对多命名关系_C#_Ef Code First_Foreign Keys_Entity Framework 5_Code First - Fatal编程技术网

C# 实体框架5代码优先自引用多对多命名关系

C# 实体框架5代码优先自引用多对多命名关系,c#,ef-code-first,foreign-keys,entity-framework-5,code-first,C#,Ef Code First,Foreign Keys,Entity Framework 5,Code First,我尝试了不同的型号和配置,但没有取得任何进展。已搜索此特定用法但未成功,已找到接近但不相同的用法 我有一个与其他产品相关的产品,到目前为止,如果只有一个关系,例如“相关产品”,则效果良好 添加其他关系(例如“相似”)时会出现问题 我想要的是一个自引用命名关系,理想情况下是一个三列外键表;ProductId,RelatedProductId,RelationId 一个产品可以有多个命名关系,一个关系可以有一个或多个产品(相关产品)。我希望避免多次定义关系的名称 一个想法如下: public cla

我尝试了不同的型号和配置,但没有取得任何进展。已搜索此特定用法但未成功,已找到接近但不相同的用法

我有一个与其他产品相关的产品,到目前为止,如果只有一个关系,例如“相关产品”,则效果良好 添加其他关系(例如“相似”)时会出现问题

我想要的是一个自引用命名关系,理想情况下是一个三列外键表;ProductId,RelatedProductId,RelationId

一个产品可以有多个命名关系,一个关系可以有一个或多个产品(相关产品)。我希望避免多次定义关系的名称

一个想法如下:

public class Product
{
    public int Id { get; set; }
    public string Title { get; set; }
    public virtual ICollection<Relation> Relations { get; set; }
}

public class Relation
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}
公共类产品
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共虚拟ICollection关系{get;set;}
}
公共阶级关系
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection产品{get;set;}
}
另一个想法是使用一个特殊的表格:

public class Product
{
    public int Id { get; set; }
    public string Title { get; set; }
    public virtual ICollection<ProductRelation> Relations { get; set; }
}

public class Relation
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ProductRelation> Products { get; set; }
}

public class ProductRelation
{
    [Key, Column(Order = 0)]
    public virtual int ProductId { get; set; }
    [Key, Column(Order = 1)]
    public virtual int RelatedProductId { get; set; }
    [Key, Column(Order = 2)]
    public virtual int RelationId { get; set; }

    [ForeignKey("ProductId")]
    public virtual Product Product { get; set; }
    [ForeignKey("RelatedProductId")]
    public virtual Product RelatedProduct { get; set; }
    [ForeignKey("RelationId")]
    public virtual Relation Relation { get; set; }
}
公共类产品
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共虚拟ICollection关系{get;set;}
}
公共阶级关系
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection产品{get;set;}
}
公共类产品关系
{
[键,列(顺序=0)]
公共虚拟int ProductId{get;set;}
[键,列(顺序=1)]
公共虚拟int-RelatedProductId{get;set;}
[键,列(顺序=2)]
公共虚拟int关系ID{get;set;}
[外键(“产品ID”)]
公共虚拟产品产品{get;set;}
[ForeignKey(“RelatedProductId”)]
公共虚拟产品关联产品{get;set;}
[ForeignKey(“RelationId”)]
公共虚拟关系{get;set;}
}
上面的示例是想要的结果布局,我希望在检索时如何访问它,很可能需要以这种方式创建。 从db的角度来看,下面的三列映射表似乎更清晰

我的问题是:

  • 推荐的方法是什么?为什么
  • 如何正确设置带有键的映射,以便数据库尽可能地理解这些关系

您的第一个解决方案看起来应该对我有效,实际发生了什么?关系应该是单向的,p1->p2不应该自动成为关系p2->p1。我明白了。在这种情况下,似乎无法摆脱“中间”表/实体。也许:ProductId、RelationId、方向作为主键?方向可以是一个位(0==产品->关系,1==关系->产品)。不过,我不确定您是否能够在代码中完成连接。