Entity Framework 数据库映射关系(使用 Seed() 方法创建重复项) [英] Entity Framework database mapping relationships (Duplicate creation using Seed() method)
问题描述
我创建了一个帖子,其中包含一个问题 和 另一个问题.这些可以作为参考,但我认为它们已处理.
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 个产品可以有多个 PurchasePrice,但 1 个 PurchasePrice 只能存在于 1 个产品中(与 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 和一个具有值的 Product 引用此 PurchasePrice 并且有我的副本.
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,它将保存 1 个合同的产品-供应商关系的金额:
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).
这篇关于Entity Framework 数据库映射关系(使用 Seed() 方法创建重复项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!