2016-01-13 21 views
0

我試圖用這種種子方法;如何使用種子方法爲我的上下文創建初始數據?

 context.Reeves.AddOrUpdate(
      p => new { p.FirstName, p.LastName }, 
      new Reeve { FirstName = "A", LastName = "A" }, 
      new Reeve { FirstName = "B", LastName = "B" }); 
     context.SaveChanges(); 
     context.Districts.AddOrUpdate(
      p => p.Name, 
      new District() { Name = "X", ReeveId = context.Reeves.First(r => r.FirstName == "A" && r.LastName == "A").Id }, 
      new District() { Name = "Y", ReeveId = context.Reeves.First(r => r.FirstName == "B" && r.LastName == "B").Id }); 
     context.SaveChanges(); 

我收到錯誤消息「的INSERT語句衝突與外鍵約束‘FK_dbo.District_dbo.Reeve_Id’。該衝突發生於數據庫‘ProjectTracking’,表‘dbo.Reeve’,列「 ID'。」

如果我更改下面的代碼;

 context.Districts.AddOrUpdate(
      p => p.Name, 
      new District() { Name = "X", Reeve = context.Reeves.First(r => r.FirstName == "A" && r.LastName == "A") }, 
      new District() { Name = "Y", Reeve = context.Reeves.First(r => r.FirstName == "B" && r.LastName == "B") }); 
     context.SaveChanges(); 

錯誤消息disapear但是當我檢查各區表我看到所有ReeveId列均爲0

什麼是我的錯誤,任何想法?

PS:我不想創建內聯Reeve的區內的AddOrUpdate方法。就像是; context.Districts.AddOrUpdate(p => p.Name,new District(){Name =「X」,Reeve = new Reeve(){FirstName =「A」,LastName =「A」});

我的實體

public class Reeve 
{ 
    public int Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string FullName 
    { 
     get { return string.Format("{0} {1}", FirstName, LastName); } 
    } 
    public virtual District District { get; set; } 
    public byte[] RowVersion { get; set; } 
} 

public class District 
{ 
    public District() 
    { 
     Projects = new HashSet<Project>(); 
     ProjectRequests = new HashSet<ProjectRequest>(); 
    } 

    public int Id { get; set; } 
    public string Name { get; set; } 
    public int ReeveId { get; set; } 
    public virtual Reeve Reeve { get; set; } 
    public byte[] RowVersion { get; set; } 
    public virtual ICollection<Project> Projects { get; set; } 
    public virtual ICollection<ProjectRequest> ProjectRequests { get; set; } 
} 

實體配置

public class ReeveConfiguration : EntityTypeConfiguration<Reeve> 
    { 
     public ReeveConfiguration() 
     { 
      HasKey<int>(p => p.Id); 
      Ignore(p => p.FullName); 
      Property(p => p.FirstName).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("FullName", 1) { IsUnique = true })).HasMaxLength(50).IsRequired(); 
      Property(p => p.LastName).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("FullName", 2) { IsUnique = true })).HasMaxLength(50).IsRequired(); 
      Property(p => p.RowVersion).IsRowVersion(); 
     } 
    } 

public class DistrictConfiguration : EntityTypeConfiguration<District> 
    { 
     public DistrictConfiguration() 
     { 
      HasKey<int>(p => p.Id); 
      HasRequired(p => p.Reeve).WithOptional(p => p.District); 
      HasMany(p => p.Projects).WithRequired(p => p.District).HasForeignKey(p => p.DistrictId); 
      HasMany(p => p.ProjectRequests).WithRequired(p => p.District).HasForeignKey(p => p.DistrictId); 
      Property(p => p.Name).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsUnique = true })).HasMaxLength(100).IsRequired(); 
      Property(p => p.RowVersion).IsRowVersion(); 
     } 
    } 

我恨實體框架球隊的1對1級的關係的規則。委託實體必須與原則實體具有相同的名稱PK,並且委託實體PK也必須是FK。這一定是個笑話。所有1對1的關係不能像Person - > PersonPhoto或Car - >方向盤。我是對的還是我誤解了他們的邏輯。例如,我也有項目和項目請求實體,項目的請求和請求的項目可以爲空我的意思是他們有0..1到0..1的關係,他們必須是自己的PK ID。還有,如果我有實體基類有Id主鍵字段。我如何從中導出我的1對1關係實體。

+0

你的模特是什麼樣的?你的錯誤提到Reeve_Id,那麼你說ReeveId列是零。如果你在區內同時擁有ReeveId和Reeve_Id,那麼你的關係是不正確的。 –

+0

我使用實體及其配置更新了我的帖子。另外,如果我的1對1關係像你說的那樣是錯誤的,那麼我想分享我的其他實體,它們是項目和projectrequest,它們的關係是0..1到0..1。 –

回答

0

好的,我自己解決了我的問題。讓我們再談一次我的期望。我想爲我的實體編寫基類,首先它只包含Id屬性。另外我想控制我的外鍵名稱,我不想自動爲我做。

也讓我們來談談收到的錯誤。我無法種子數據並接收到錯誤,因爲我自動創建外鍵,並在區域實體ReeveId列中創建類似其他數據列的東西。因此,當我設置District.ReeIdId與現有的reeve和保存更改,因爲拋出外鍵錯誤。

我做了以下更改我的代碼,以解決對ef和我的期望問題;

  • 從區實體刪除ReeveId因爲沒有必要
  • 添加HasRequired(p值=> p.Reeve).WithOptional(p值=> p.District).MAP(M => m.MapKey(」 ReeveId「));代碼到我的分區配置

所以這兩個表都包含自己的Id列作爲主鍵,分區也有ReeveId列作爲外鍵。結果是和我的期望相符。

相關問題