简体   繁体   English

代码优先:同一实体的一对多关系

[英]Code first: One to many relationship of the same entity

I have an Entity Product which can have multiple additional products. 我有一个实体Product ,可以有多个其他产品。 So what I need is a junction table to map a Product to additional Product s. 因此,我需要一个联结表将一个Product映射到其他Product

What I tried to do is the following: 我试图做的是以下几点:

[Table("Product")]
public class Product : EntityBase
{
    [Key]
    public int Id { get; set; }

    public virtual ICollection<Argument> Arguments { get; set; }
    public virtual ICollection<Product> AdditionalProducts { get; set; }
}

Add a collection of Products, hoping that EF would build a junction table. 添加产品集合,希望EF可以建立联结表。 However I end up with this in my migration: 但是,在迁移过程中最终遇到了这个问题:

public partial class test1 : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.Product", "Product_Id", c => c.Int());
        CreateIndex("dbo.Product", "Product_Id");
        AddForeignKey("dbo.Product", "Product_Id", "dbo.Product", "Id");
    }

    public override void Down()
    {
        DropForeignKey("dbo.Product", "Product_Id", "dbo.Product");
        DropIndex("dbo.Product", new[] { "Product_Id" });
        DropColumn("dbo.Product", "Product_Id");
    }
}

What am I doing wrong here? 我在这里做错了什么?

As you said in the comment that: 正如您在评论中所说:

A product can be an additional product to multiple products. 一个产品可以是多个产品的附加产品。 And can also have its set of additional products. 并且还可以拥有其一套其他产品。

So, First make a model class named AdditionalProduct as follows: 因此,首先制作一个名为AdditionalProduct的模型类,如下所示:

[Table("AdditionalProduct")]
public class AdditionalProduct
{
    [Key]
    public int Id { get; set; }

    public int ProductId {get; set;}
    public int AdditionalProductId {get; set;}

    public Product Product {get; set;}
    public Product AdditionalProduct {get; set;}
}

And your Product class should as follows: 并且您的Product类应如下所示:

[Table("Product")]
public class Product : EntityBase
{
    [Key]
    public int Id { get; set; }

    .......

    public virtual ICollection<AdditionalProduct> HisAdditionalProducts { get; set; }
    public virtual ICollection<AdditionalProduct> AdditionalProductsTo { get; set; }
}

Then in the OnModelCreating as follows: 然后在OnModelCreating中如下所示:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<AdditionalProduct>()
                .HasRequired(m => m.Product)
                .WithMany(t => t.HisAdditionalProducts)
                .HasForeignKey(m => m.ProductId)
                .WillCascadeOnDelete(false);

    modelBuilder.Entity<AdditionalProduct>()
                .HasRequired(m => m.AdditionalProduct)
                .WithMany(t => t.AdditionalProductsTo)
                .HasForeignKey(m => m.AdditionalProductId)
                .WillCascadeOnDelete(false);
}

The great solution for you is: 您的最佳解决方案是:

First, make a new class for all your Aditional Productions 首先,为您的所有Aditional Productions新的班级

[Table("AdditionalProducts")]
public class AdditionalProduct
{
    [Key]
    public int Id { get; set; }

    public virtual ICollection<Product> AdditionalProducts { get; set; }
}

Second, create a relationship one-to-one to this class 其次,与该类one-to-one建立relationship

[Table("Product")]
public class Product
{
    [Key]
    public int Id { get; set; }

    public virtual ICollection<Argument> Arguments { get; set; }//your business

    public int AdditionalProductsId { get; set; }
    public virtual AdditionalProduct AdditionalProducts { get; set; }
}

See more one-to-one 一对一查看更多

Hope to be useful for you! 希望对您有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM