实体框架数据库映射关系(使用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个产品可以有多个PurchasePrices,但是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; }
}
现在,让我们向其添加供应商实体,因为这就是为什么我将Sales&分开购买,不要从中创建枚举,因为1个产品(在数据库中)可以有多个供应商,每个供应商具有自己的销售/购买价格 AND 和另一个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; }
}
最后,我有一个合同实体,该实体具有以下结构:
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的产品,并且有我的副本.
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,更改e映射(Fluent AND批注),更改我播种和初始化所有东西的方式,定义了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.
我的问题:
- Why is this duplicate occuring/appearing ?
If i want to be able to have 1 instance holding the relation of Price-Supplier-Product-Contract, how should i do this?Answer is here
推荐答案
我通过重新设计模型解决了我的问题.我添加了一个附加的 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).
这篇关于实体框架数据库映射关系(使用Seed()方法重复创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!