[英]Entity Framework 1 to Many creates new objects
I'm using EntityFramework for my Microsoft Sql Data Base. 我正在为我的Microsoft Sql数据库使用EntityFramework。 First entity is Product : 第一个实体是Product :
public class Product
{
public Product()
{
ProductStories = new HashSet<ProductStory>();
}
public int ProductId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Deleted { get; set; }
public HashSet<ProductStory> ProductStories { get; set; }
}
And another entity is ProductStory , which stores story about income or outcome of Products . 而另一家实体ProductStory,存储故事产品的收入或结果。
public class ProductStory
{
public int ProductStoryId { get; set; }
public virtual Product.Product Product { get; set; }
public int Count { get; set; }
public DateTime DateTime { get; set; }
}
So one Product could be in mane ProductStories , or in none. 因此,一个产品可能在鬃毛产品 故事中 ,也可能不在。
I will not show all code(too big), so when I firstly create a single Product instance and save it in DB. 我不会显示所有代码(太大),因此当我第一次创建一个Product实例并将其保存在DB中时。 Then I create a single ProductStory and reference to property Product to that instance of Product. 然后,我创建一个ProductStory,并引用属性Product到该Product实例。 Then I save this ProductStory , there becomes 2 instances of ProductStory . 然后我保存此ProductStory ,有2个ProductStory实例。 As I read, and I made this as virtual property: 在我阅读时,我将其作为虚拟属性:
public virtual Product.Product Product { get; set; }
How this problem could be solved? 这个问题怎么解决?
I'm using EntityTypeConfiguration
for tables configuration. 我正在使用EntityTypeConfiguration
进行表配置。
public class ProductMap : EntityTypeConfiguration<Product>
{
public ProductMap()
{
ToTable("Products").HasKey(x => x.ProductId);
Property(x => x.ProductId).IsRequired();
Property(x => x.Name).IsRequired().HasMaxLength(255).HasColumnName("Name");
//.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_Name") { IsUnique = true }));
Property(x => x.Description).IsOptional().HasColumnName("Description");
Property(x => x.Deleted).HasColumnName("Deleted");
}
}
And for ProductStory: 对于ProductStory:
class ProductStoryMap: EntityTypeConfiguration<ProductStory>
{
public ProductStoryMap()
{
ToTable("ProductStories").HasKey(ps => ps.ProductStoryId);
Property(ps => ps.ProductStoryId).IsRequired();
//Property(ps => ps.ProductId).IsRequired().HasColumnName("ProductId");
Property(ps => ps.Count).HasColumnName("Count");
Property(ps => ps.DateTime).HasColumnName("DateTime");
}
}
You have some errors in your code: 您的代码中有一些错误:
//Change this:
public HashSet<ProductStory> ProductStories { get; set; }
//For this (virtual is needed here, also use ICollection rather than any specific implementation)
public virtual ICollection<ProductStory> ProductStories { get; set; }
//Change this:
public virtual Product.Product Product { get; set; }
//For this (virtual makes no sense here)
public Product.Product Product { get; set; }
And lastly, ProductStory
needs a way to keep the reference to its parent Product
. 最后, ProductStory
需要一种方法来保留对其父Product
的引用。 This is what creates the Foreign Key relationship in your database and allows Entity Framework to link the tables. 这就是在数据库中创建外键关系并允许实体框架链接表的原因。 So add this to ProductStory
: 因此,将其添加到ProductStory
:
public int ProductId { get; set; }
If you are still getting a duplicated object (which may happen), ensure you are setting the ProductId
to the ProductStory
you are saving. 如果仍然有重复的对象(可能会发生),请确保将ProductId
设置为要保存的ProductStory
。
The solution was about Entity Framework "bug/feature". 解决方案是关于实体框架“错误/功能”。 As I add new ProductStory
into DataBase, it attaches the whole graph(including all other entities references and recreates them). 当我向数据库中添加新的ProductStory
,它将附加整个图(包括所有其他实体引用并重新创建它们)。 So before commiting new ProductStory
, I have to set to null all it's navigation properties to avoid recreating. 因此,在提交新的ProductStory
之前,我必须将其所有导航属性设置为null以避免重新创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.