实体框架数据库映射关系(使用Seed()方法重复创建)

时间:2018-12-11 09:58:23

标签: asp.net-mvc orm entity-framework-6 relational-database code-first

我用an issueanother issue创建了一条信息。 这些可以作为参考,但我认为它们已处理。

由于这些问题以及我(需要或不需要)采取的措施引起的问题困扰着我,因为我不太了解EF的行为和期望。

我有一个Product,PurchasePrice和SalesPrice实体,我最初的想法是1个产品可以具有多个PurchasePrices,但是1个PurchasePrice只能存在于1个产品中(与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; }
}

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

所以上面变成了:

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 ”的实体结构:

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; }
}

最后,我有一个合同实体,该实体具有以下结构:

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的产品,并且有我的副本。

将实体产品的PurchasePrice类中的关系更改为ICollection不会创建重复项,但我不希望此集合,因为它不是“多对多”关系...

我已经尝试了很多方法,但是没有任何方法可以“解决”此问题(如果这是一个开始的问题,对我来说是,但对于EF而言可能不是),例如删除继承BaseEntity,更改e映射(Fluent AND注释),我播种和初始化所有东西的方式,定义了ID的我自己,你就把它命名为...

请注意,其目的不是要优化我的播种方式,而是要拥有一个像样的工作模型并理解EF的作用和作用。

我的问题:

  • 为什么此重复出现/出现?
  • 如果我希望能够拥有1个实例,其中包含 价格供应商产品合同,我应该怎么做? Answer is here

1 个答案:

答案 0 :(得分:0)

我通过重新设计模型解决了我的问题。我添加了一个附加的 ProductForSupplier 实体,其中包含产品与供应商和产品编号的关系。

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

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

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

添加了一个实体 ProductsForContract ,该实体将持有1个合同的“产品与供应商”关系的金额:

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; }
}

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