我有2個實體User和User_Profile(一對一關係)。使用Auto-Id的實體框架外鍵插入
public class User
{
[Key]
[ForeignKey("user_profile")]
public int user_id {get;set;}
public string username {get;set;}
public string email {get;set;}
public virtual User_Proile user_profile {get;set;}
}
public class User_Profile
{
[Key]
public int user_id {get;set;}
public string firstname {get;set;}
public string lastname {get;set;}
}
user_id是兩個SQL Server的用戶和User_Profile表中的一個PK:我有如下聯繫他們。它也被設置爲用戶表中的標識列。
當我嘗試通過EFDBContext Add/SaveChanges插入新記錄時。我收到以下錯誤:「user_rofile表中的user_id不能爲NULL」這非常合理,因爲這是一個PK列。我希望EF能夠從用戶身份獲取身份user_id,並在保存時將其插入User_Profile user_id。
這是可能的,如果是的話,我將如何實現?
更新:請注意,我手動創建了數據庫表和代碼類,所以我沒有通過.edmx文件訪問StoreGeneratedPattern。
哇!正如你所說的那樣。許多感謝Slauma! – chrisg229