2017-02-27 272 views
9

我是一名綠色的asp.net開發人員,我正在使用最新的實體框架與我的項目。我遇到了一個問題,我的數據庫採用自動生成的值(我認爲)。這是確切的錯誤。INSERT語句與FOREGIGN KEY約束衝突

enter image description here

下面的代碼:

public class Address 
{ 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int AddressId { get; set; } 
// some more properties 
} 

public class ApplicationUser : IdentityUser 
{ 
     [ForeignKey("Address")]   
    public int AddressId { get; set; } 
    public Address Address { get; set; } 

// some more properties 
} 

public class Shelter 
{ 
     [Required] 
     [ForeignKey("Address")] 
     public int AddressId { get; set; } 
     public Address Address { get; set; } 
// some more properties 
} 
//seed 
private void CreateShelters() 
{ 
    EntityEntry<Address> MspcaAddress = DbContext.Addresses.Add(new Address() 
    { 
       Street = "350 S Huntington Ave", 
       City = "Jamaica Plain", 
       State = "MA", 
       AreaCode = "02130", 
       Latitude = 42.3228928, 
       Longititude = -71.11120540000002 
    }); 
    EntityEntry<Address> BostonAnimalCareAndControlAddress = DbContext.Addresses.Add(new Address() 
    { 
       Street = "26 Mahler Rd", 
       City = "Roslindale", 
       State = "MA", 
       AreaCode = "02131", 
       Latitude = 42.2943377, 
       Longititude = -71.12153390000003 
      }); 
      EntityEntry<Address> AnimalRescueLeagueOfBostonAddress = DbContext.Addresses.Add(new Address() 
      { 
       Street = "10 Chandler St", 
       City = "Boston", 
       State = "MA", 
       AreaCode = "0S2116", 
       Latitude = 42.3470486, 
       Longititude = -71.06976929999996 
      }); 

      EntityEntry<Shelter> Mspca = DbContext.Shelters.Add(new Shelter() 
      { 
       Name = "MCSPA", 
       AddressId = MspcaAddress.Entity.AddressId 
      }); 
      EntityEntry<Shelter> BostonAnimalCareAndControl = DbContext.Shelters.Add(new Shelter() 
      { 
       Name = "Boston Animal Care And Control", 
       AddressId = BostonAnimalCareAndControlAddress.Entity.AddressId 
      }); 
      EntityEntry<Shelter> AnimalRescueLeagueOfBoston = DbContext.Shelters.Add(new Shelter() 
      { 
       Name = "Animal Rescue League Of Boston Address", 
       AddressId = AnimalRescueLeagueOfBostonAddress.Entity.AddressId 
      }); 
      DbContext.SaveChanges(); 
    } 

我試圖重新創建數據庫,查看是否有數據不一致/表的問題,但我仍然得到錯誤。有任何想法嗎?

+2

舉一個賞金並不會讓問題變得更好。您仍然需要提供[mcve]以避免後續評論,例如*我仍然收到錯誤... *。由於異常消息清楚地表明問題不在所提供的代碼中(儘管很可能它有相同的問題),但是在插入'ApplicationUser'的代碼中。 –

+0

您提供的代碼根本不會將新的'ApplicationUser'添加到數據庫,而錯誤表明外鍵被違反了'ApplicationUser'。你確定你正在調試和共享相同的代碼。如果發佈的代碼不正確,請重新查看發佈的代碼。 –

回答

4

您嘗試播種的「Shelter」實體要求您提交現有的AddressID上面的代碼嘗試獲取尚未創建的地址的AddressId。

您必須先提交到數據庫中的地址(使用DbContext.SaveChanges();

然後,您可以創建與檢索到的AddressId住房的實體。下面的代碼顯示瞭如何執行此操作的示例,但是您可能必須重新編寫提取addressId的查詢。或者,您可能必須將它們與插入過程完全分開。

private void CreateShelters() 
{ 
    EntityEntry<Address> MspcaAddress = DbContext.Addresses.Add(new Address() 
    { 
       Street = "350 S Huntington Ave", 
       City = "Jamaica Plain", 
       State = "MA", 
       AreaCode = "02130", 
       Latitude = 42.3228928, 
       Longititude = -71.11120540000002 
    }); 
    EntityEntry<Address> BostonAnimalCareAndControlAddress = DbContext.Addresses.Add(new Address() 
    { 
       Street = "26 Mahler Rd", 
       City = "Roslindale", 
       State = "MA", 
       AreaCode = "02131", 
       Latitude = 42.2943377, 
       Longititude = -71.12153390000003 
      }); 
      EntityEntry<Address> AnimalRescueLeagueOfBostonAddress = DbContext.Addresses.Add(new Address() 
      { 
       Street = "10 Chandler St", 
       City = "Boston", 
       State = "MA", 
       AreaCode = "0S2116", 
       Latitude = 42.3470486, 
       Longititude = -71.06976929999996 
      }); 

    DbContext.SaveChanges(); 


      EntityEntry<Shelter> Mspca = DbContext.Shelters.Add(new Shelter() 
      { 
       Name = "MCSPA", 
       AddressId = MspcaAddress.Entity.AddressId 
      }); 
      EntityEntry<Shelter> BostonAnimalCareAndControl = DbContext.Shelters.Add(new Shelter() 
      { 
       Name = "Boston Animal Care And Control", 
       AddressId = DbContext.Addresses.Where(x => x.Street == "26 Mahler Rd").FirstOrDefault().AddressId 
     }); 
      EntityEntry<Shelter> AnimalRescueLeagueOfBoston = DbContext.Shelters.Add(new Shelter() 
      { 
       Name = "Animal Rescue League Of Boston Address", 
       AddressId = DbContext.Addresses.Where(x => x.Street == "10 Chandler St").FirstOrDefault().AddressId 

      }); 
      DbContext.SaveChanges(); 
    } 

希望這足以讓你走了。如果addressId的查詢不起作用,請告訴我,我很樂意爲您提供幫助。

+0

如上所述分開操作,但我得到相同的錯誤。 'Microsoft.EntityFrameworkCore.DbUpdateException:更新條目時發生錯誤。詳情請參閱內部例外。 ---> System.Data.SqlClient。SqlException:INSERT語句與FOREIGN KEY約束「FK_ApplicationUser_Address_AddressId」衝突。衝突發生在數據庫「WalkFred-12340be6-0442-4622-b782-a7412bb7d321」,表「dbo.Address」,列'AddressId'。' – user664509

+0

我已經編輯瞭如何檢索有效AddressId示例的代碼。你可以修改,以獲得正確的Addressid – DaniDev

+0

我複製並粘貼到我的項目中的代碼,我收到相同的錯誤。 – user664509

2

這個問題的關鍵:)是使用引用屬性,而不是Id值。這意味着:設置Address而不是AddressId。當EF保存更改時,它會找出實體之間的依賴關係並將它們全部保存爲正確的順序,同時將生成的AddressId關鍵值恰好作爲外鍵值進行設置。

你代碼的實質(即離開了財產分配,爲簡潔起見)應該是這樣的:

var mspcaAddress = new Address(); 
var bostonAnimalCareAndControlAddress = new Address(); 
var animalRescueLeagueOfBostonAddress = new Address(); 
context.Addresses.AddRange(new[] 
{ 
    mspcaAddress, 
    bostonAnimalCareAndControlAddress, 
    animalRescueLeagueOfBostonAddress 
}); 

var mspca = new Shelter() 
{ 
    Address = mspcaAddress 
}; 
var bostonAnimalCareAndControl = new Shelter() 
{ 
    Address = bostonAnimalCareAndControlAddress 
}; 
var animalRescueLeagueOfBoston = new Shelter() 
{ 
    Address = animalRescueLeagueOfBostonAddress 
}; 
context.Shelters.AddRange(new[] 
{ 
    mspca, 
    bostonAnimalCareAndControl, 
    animalRescueLeagueOfBoston 
}); 
context.SaveChanges(); 

雖然您顯示錯誤適用於ApplicationUserAddressId,我想在一個類似的方法創建ApplicationUser即可解決問題。

順便說一句,您可以不用第一個context.Addresses.AddRange聲明。地址將通過添加避難所而添加。

請注意,我也從代碼中刪除了EntityEntry。我認爲他們不必要地使代碼複雜化。你只需要你正在創建的簡單對象。

+1

只是爲了記錄,我沒有投下你的答案。我昨天真的注意到了,我喜歡它! (假設它工作,我沒有測試過)。順便說一句,你也不會顯示如何播種地址字段。沒什麼大不了的,但其中一些可能是必需的(非空)。 – DaniDev

相關問題