在同一张表上多对多的DotNetCore实体框架

时间:2018-06-25 21:04:29

标签: c# many-to-many entity-framework-core

我正在使用DotNetCore 2,我想实现一个只有单个模型的场景,该模型可以包含零个或多个相同模型。

为简单起见,请考虑以下情形:

  • 引擎可以由许多零件(组件)组成
  • 每个零件可以包含零个或多个其他零件(组件)

我想这样链接这些项目:

系统

Id    Name
----------------
1     Drive System

组件

Id    Name
----------------
1     Motor
2     Bearings
3     Couplings
4     Fixtures

SystemComponent之间的多对多关系如下:

系统组件

SystemId    ComponentId
-------------------------
1           1

Component上的多对多链接(由于缺少更好的更好的名称)

ComponentComponents

ParentId    ChildId
---------------------
1           2
1           3
1           4

因此,在上述情况下,可以翻译为驱动系统具有一个组件-电动机,并且电动机具有其他子组件“轴承”,“联轴器”和“固定装置” < / p>

SytemComponent关系正常。对于ComponentComponent关系,我在代码优先迁移中尝试了以下方法:

组件

public class Component 
{
    public Component() 
    {
        SystemComponents = new Collection<SystemComponent>();
        ChildComponents = new Collection<Component>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    // Needs this for the Many to Many relationship
    public virtual ICollection<SystemComponent> SystemComponents { get; set; }

    // Any component can contain one or more existing components
    public virtual ICollection<Component> ChildComponents { get; set; }
}

ComponentComponent 链接模型:

public class ComponentComponent
{
    public int ParentComponentID { get; set; }
    public int ChildComponentID { get; set; }

    public Component ParentComponent { get; set; }
    public Component ChildComponent { get; set; }
}

使用我的ComponentComponent配置:

public class ComponentComponentConfiguration : IEntityTypeConfiguration<ComponentComponent> {
    public void Configure(EntityTypeBuilder<ComponentComponent> builder) {
        builder.ToTable("ComponentComponent");

        builder.HasKey(cc => new { cc.ParentComponentID, cc.ChildComponentID });

        builder.HasOne(cc => cc.ParentComponent)
            .WithMany(c => c.ComponentComponents)
            .HasForeignKey(cc => cc.ParentComponentID);

        builder.HasOne(cc => cc.ChildComponent)
            .WithMany(c => c.ComponentComponents)
            .HasForeignKey(cc => cc.ChildComponentID);
    }
}

运行迁移时,出现以下错误:

  

无法在'Component.ComponentComponents'和'ComponentComponent.ChildComponent'之间创建关系,因为'Component.ComponentComponents'和'ComponentComponent.ParentComponent'之间已经存在关系。导航属性只能参与单个关系。

如何在DotNetCore 2的Entity Framework中实现这种关系?

编辑

我已经在我的github页面上上传了一个类似的示例项目:

https://github.com/JAspeling/Many-to-Many

1 个答案:

答案 0 :(得分:0)

问题似乎出在Component类中。而不是引用组件集合,它应该看起来像这样

public class Component 
{
    public Component() 
    {
        SystemComponents = new Collection<SystemComponent>();
        ChildComponents = new Collection<ComponentComponent>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    // Needs this for the Many to Many relationship
    public virtual ICollection<SystemComponent> SystemComponents { get; set; }

    // Any component can contain one or more existing components
    public virtual ICollection<ComponentComponent> ChildComponents { get; set; }
}

您需要引用映射表。

https://docs.microsoft.com/de-de/ef/core/modeling/relationships#many-to-many