简体   繁体   English

如何播种新的联结表

[英]How to seed new junction table

I am using Code first in Entity framework. 我首先在实体框架中使用Code。 There are two tables in my database - Clients and Products. 我的数据库中有两个表-客户和产品。 There is some data in it. 里面有一些数据。 I have added a new, junction table that has foreign keys to both of them. 我添加了一个新的联结表,它们都具有外键。 How should I seed that table? 我应该如何播种那个桌子? And will Entity framework add new rows when I add new Client or Product, because it seems that it doesn't.: 当我添加新的Client或Product时,Entity Framework会添加新行,因为似乎没有。

public class UserPriceList
    {
        public UserPriceList()
        {
            PriceFactor = 1M;
        }

        [Key]
        public int UserPriceListId { get; set; }

        [Index("IX_UserPrice", 1)]
        public int ProductId { get; set; }
        public virtual Product Product { get; set; }

        [Index("IX_UserPrice", 2)]
        public int ClientId { get; set; }
        public virtual Client Client { get; set; }

        public decimal PriceFactor { get; set; }
    }

Your UserPriceList looks a lot like a junction table, but EntityFramework is not viewing it that way because you defined it as an entity with additional properties. 您的UserPriceList看起来很像一个联结表,但是EntityFramework并不以这种方式查看它,因为您已将其定义为具有其他属性的实体。 A junction table is automatically created behind the scenes as a table with ProductId and ClientId by adding an ICollection to the Client and ICollection to the product. 通过将ICollection添加到Client并将ICollection添加到产品,将在幕后自动创建一个带有ProductId和ClientId的表,作为具有ProductId和ClientId的表。 There is no defined model, you would interact with it by Client.Products.Add(someProduct) and it would populate the junction table behind the scenes. 没有定义的模型,您可以通过Client.Products.Add(someProduct)与它进行交互,并且它将在幕后填充联结表。

To get your UserPriceList working as a junction table you could try something like this in your OnModelCreating 为了使您的UserPriceList可以作为联结表,您可以在OnModelCreating中尝试类似的方法

modelBuilder.Entity<Product>()
    .HasMany(x => x.Clients)
    .WithMany(x => x.Products)
.Map(x =>
{
    x.ToTable("UserPriceList"); // third table is named Cookbooks
    x.MapLeftKey("ProductId");
    x.MapRightKey("ClientId");
});

to explicitly map UserPriceList as the junction table. 显式地将UserPriceList映射为联结表。 You may have problems with the non nullable decimal (or maybe not since you're setting a value in the constructor) and with having the UserPriceList defined as an entity. 您可能会遇到以下问题:不可为空的十进制数(或者可能不是因为您在构造函数中设置了值),并且将UserPriceList定义为实体。

Your could also just interact with the UserProductList as an entity and explicitly add items to it. 您还可以仅作为一个实体与UserProductList进行交互,并向其中明确添加项目。

Probably the safest (as in most likely to work) way to go would be to remove the ICollection<Product> from Client and the ICollection<Client> from product add an ICollection<UserPriceList> to both. 可能最安全(最可能可行)的方法是从客户端删除ICollection <Product>,从产品删除ICollection <Client>,同时向两者添加ICollection <UserPriceList>。

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

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