实体框架数据库映射关系(使用 Seed() 方法重复创建) [英] Entity Framework database mapping relationships (Duplicate creation using Seed() method)

查看:28
本文介绍了实体框架数据库映射关系(使用 Seed() 方法重复创建)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个问题另一个问题.这些可以作为参考,但我认为它们已处理.

I created a post with an issue and another issue. These can be looked at for references but i consider them as handled.

我对这些问题提出的问题以及我(需要或不需要)应用的操作困扰着我,因为我不太了解 EF 的行为和期望.

My question arising from these issues and the action i (need or not need) to apply bothers me because i don't quite understand EF its behavior and expectations.

我有一个 Product、PurchasePrice 和 SalesPrice 实体,我最初的想法是 1 个 Product 可以有多个 PurchasePrice,但 1 个 PurchasePrice 只能存在于 1 个 Product 中(SalesPrice 也是如此).

I have a Product, PurchasePrice and SalesPrice entity where my initial thought was that 1 Product can have multiple PurchasePrices but that 1 PurchasePrice only can exist in 1 Product (same for SalesPrice).

因此这些关系:

// NOTE that BaseEntity just has a "int ID" prop and datetimes/stamps
public class Product : BaseEntity
{
   public  ICollection<PurchasePrice> PurchasePrices { get; set; }
   public  ICollection<PurchasePrice> SalesPrices { get; set; }
}

public  class PurchasePrice:BaseEntity
{      
   public Product Product { get; set; }
}

public  class SalesPrice:BaseEntity
{      
   public Product Product { get; set; }
}

现在,让我们添加一个供应商实体,因为这就是我将销售和销售分开的原因.分开购买,不要从中创建枚举,因为 1 个产品(在数据库中)可以有多个供应商,每个供应商都有自己的销售/购买价格另一个 Productnumber 值.

Now, lets add a Supplier Entity to it because that is why i seperate Sales & Purchase apart and don't create an Enum out of it, because 1 Product (in database) can have multiple suppliers, each having their own Sales/Purchase prices AND another Productnumber value.

所以上面变成了:

public class Product : BaseEntity
{
   public  ICollection<PurchasePrice> PurchasePrices { get; set; }
   public  ICollection<PurchasePrice> SalesPrices { get; set; }
   // added
   public  ICollection<Supplier> Suppliers { get; set; }
}

public  class PurchasePrice:BaseEntity
{      
   public Product Product { get; set; }
   // added
   public Supplier Supplier { get; set; }
}

public  class SalesPrice:BaseEntity
{      
   public Product Product { get; set; }
   // added
   public Supplier Supplier { get; set; }
}

// added Entity Supplier into the party
public class Supplier : BaseEntity
{
    public ICollection<Product> Products { get; set; }
    public ICollection<PurchasePrice> PurchasePrices { get; set; }
    public ICollection<SalesPrice> SalesPrices { get; set; }
}

让我们继续说下去,因为它并不止于此,我想跟踪这些产品-供应商-价格关系,所以我创建了一个名为ProductSupplierForContract"的实体,它具有以下内容结构:

Lets continue a little furhter because it doesn't stop there, i want to keep track of these Product-Supplier-Prices relations so i created a Entity called 'ProductSupplierForContract' which would have the following structure:

public class ProductSupplierForContract:BaseEntity
{
    public string ProductnumberValue { get; set; }

    public int Product_Id { get; set; }
    public int Supplier_Id { get; set; }
    public int? Contract_Id { get; set; }

    public virtual Product Product { get; set; }
    public virtual Supplier Supplier { get; set; }
    public virtual Contract Contract { get; set; }
}

最后我有一个 Contract 实体,它具有以下结构:

Finally i have a Contract Entity which has the following structure:

public class Contract:BaseEntity
{
  [Required]
  public ICollection<Product> Products { get; set; }
  public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
}

所以产品变成:

public class Product : BaseEntity
{
   public  ICollection<PurchasePrice> PurchasePrices { get; set; }
   public  ICollection<PurchasePrice> SalesPrices { get; set; }    
   public  ICollection<Supplier> Suppliers { get; set; }
   // added
   public  ICollection<Contract> Contracts { get; set; }
}   

自定义播种(从 DropCreateDatabaseAlways 继承):

protected override void Seed(ApplicationDbContext context)
{
   PurchasePrice purchaseprice = new PurchasePrice((decimal)17.70);
   ctx.PurchasePrices.Add(purchaseprice);

   Product product1 = new Product("test product 1",purchaseprice);
   ctx.Products.Add(product1);

   base.Seed(ctx);
}

我还在 Fluent API 中定义了映射:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
         // setting the Product FK relation required + related entity
        modelBuilder.Entity<Entity.ProductSupplierForContract>().HasRequired(psfc => psfc.Product)
                                                            .WithMany(p => p.ProductSupplierForContracts)
                                                            .HasForeignKey(psfc => psfc.Product_Id);

        // setting the Supplier FK relation required + related entity
        modelBuilder.Entity<Entity.ProductSupplierForContract>().HasRequired(psfc => psfc.Supplier)
                                                           .WithMany(s => s.ProductSupplierForContracts)
                                                           .HasForeignKey(psfc => psfc.Supplier_Id);

        // setting the Contract FK relation required + related entity
        modelBuilder.Entity<Entity.ProductSupplierForContract>().HasOptional(psfc => psfc.Contract)
                                                          .WithMany(c => c.ProductSupplierForContracts)
                                                          .HasForeignKey(psfc => psfc.Contract_Id);

    }

现在,最初我没有任何问题,我真的不明白是什么导致了这种突然的变化,我现在在为我的数据库播种时得到了重复的产品.我可以将其简化为仅添加一个带有值的简单 PurchasePrice 和一个具有此 PurchasePrice 引用的 Product ,这是我的副本.

Now, initially i didn't had any issues and i really really don't understand what has brought up this sudden change that i now got duplicates Products when i seed my database. I can strip it down to just adding a simple PurchasePrice with a value and a Product having a reference to this PurchasePrice and there is my duplicate.

将实体产品的 PurchasePrice 类中的关系更改为 ICollection 不会创建重复但我不想要这个集合,因为它不是多对多关系...

Changing the relation inside the PurchasePrice class of the Entity Product, to a ICollection doesn't create a duplicate but i don't want this collection because it is not a Many to Many relation ...

我已经尝试了很多东西,但没有什么可以解决"这个问题(如果这是一个开始的问题,对我来说是的,但对 EF 来说可能不是),比如删除继承 BaseEntity、更改映射(流利和注释)、更改我播种和初始化一切的方式,我自己定义ID,你命名它......

I have tried enormous amounts of things but nothing that "resolved" this (if this is a problem to start with, for me yes but maybe not for EF) like removing inhertance BaseEntity, changinge Mapping (Fluent AND annotations), changed the way i seeded and initialized everthing, defining ID's myself, you name it ...

请注意,这样做的目的不是优化我的播种方式,而是要有一个体面的工作模型,并了解 EF 做什么以及它想要什么.

Mind that the purpose is not to optimize the way i seed in anyway but to have a decent working Model AND to understand what EF does and what it wants.

我的问题:

  • 为什么会出现/出现这种重复?
  • 如果我希望能够有 1 个实例持有价格-供应商-产品-合同,我该怎么做? 答案在这里

推荐答案

我通过重新设计模型解决了我的问题.我添加了一个额外的实体 ProductForSupplier ,它包含 Product &供应商和产品编号.

I fixed my problem by redesigning the model. I have added a additional Entity ProductForSupplier which holds the relation of a Product & Supplier and a Productnumber.

public class ProductForSupplier:BaseEntity
{
    public string ProductnumberValue { get; set; }

    [Required]
    public Product Product { get; set; }

    [Required]
    public Supplier Supplier { get; set; }
}

添加了一个实体 ProductsForContract,它将持有一份合同的 Product-Supplier 关系的金额:

Added a Entity ProductsForContract which will hold the amount of a Product-Supplier relation for 1 contract:

public class ProductsForContract
{
    public int ProductsForContractId { get; set; }        
    public int Amount { get; set; }
    public ProductForSupplier ProductForSupplier { get; set; }
    public Contract Contract { get; set; }
}

现有实体 ProductSupplierForContract 变为:

public class ProductSupplierForContract:BaseEntity
{
    public ICollection<ProductsForContract> ProductsForContract { get; set; }

    [Required]
    public Contract Contract { get; set; }
}

这使我可以灵活地保持实体之间的任何类型的关系,并且还处理了重复项(我仍然不知道其原因).

This gives me the flexibility to keep relations of any kind between the entities and also has taken care of the duplicate (which i still don't know the cause of).

这篇关于实体框架数据库映射关系(使用 Seed() 方法重复创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆