2017-06-28 52 views
0

我爲我的Microsoft Sql數據庫使用EntityFramework。 第一實體產品Entity Framework 1 to Many創建新對象

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

並形成其他單位是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; } 
} 

所以一個產品可能是鬃毛ProductStories,或沒有。

我不會顯示所有代碼(太大),所以當我首先創建一個單一的產品實例並將其保存在數據庫中。然後,我創建一個單一的ProductStory並引用屬性Product到該產品的實例。 然後我保存這個ProductStory,變成ProductStory兩個實例。 當我看完了,我做了這個虛擬財產:

public virtual Product.Product Product { get; set; } 

如何這個問題可以得到解決?

我使用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"); 
     } 
    } 

而對於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"); 
     } 
    } 
+1

您顯示了兩次ProductStory的代碼,並且從不顯示產品的代碼。此外,您的ProductStory缺少一個ProductId來存儲產品的參考 –

+0

對不起,修復它。謝謝。 – KamikyIT

+0

您的ProductStoryMap現在需要一個外鍵到產品 –

回答

1

你在你的代碼中的一些錯誤:

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

最後,ProductStory需要一種方式來保持引用其父Product。這是在您的數據庫中創建外鍵關係並允許實體框架鏈接表。所以加這ProductStory

public int ProductId { get; set; } 

如果你仍然得到一個複製的對象(可能發生的),請確保您設置ProductIdProductStory您保存。

+0

據我所知,我得到 'productStory.ProductId = product.ProductId';代碼中的 ,對不對? – KamikyIT

+0

@KamikyIT是的,看我的更新 –

+0

我照你所說的做了,但它仍然是複製品。 – KamikyIT

0

解決方案是關於實體框架「bug /功能」。 當我將新的ProductStory添加到數據庫中時,它會附加整個圖表(包括所有其他實體引用和再創建它們)。 因此,在提交新的ProductStory之前,我必須將其全部導航屬性設置爲空以避免重新創建。

+0

因此,基本上解決方案超出了您在問題中發佈的代碼的範圍,對吧? – grek40

+0

@ grek40 50/50。 Camilo Terevinto在'EntityTypeConfiguration'中幫助了'HasRequired()'。 另一個問題是我寫的較高。 – KamikyIT

相關問題