我有User
< Country
模型。用戶屬於一個國家,但可能不屬於任何(空外鍵)。實體框架代碼優先空外鍵
我該如何設置?當我嘗試插入一個空國家的用戶時,它告訴我它不能爲空。
的模型如下:
public class User{
public int CountryId { get; set; }
public Country Country { get; set; }
}
public class Country{
public List<User> Users {get; set;}
public int CountryId {get; set;}
}
錯誤:A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = Country_Users ]"}
能不能請糾正我,如果我錯了。通過代碼第一個asp.net mvc - 5實體框架中的DEFAULT,外鍵是NULLABLE。 – Unbreakable 2017-08-09 22:34:03
如果我們想使它不可空。我們需要使用流利的API,如果沒有,然後使用「必需」屬性進行裝飾。我對麼? – Unbreakable 2017-08-09 22:34:43
如果我們不這樣做,那麼外鍵將被默認爲Nullable – Unbreakable 2017-08-09 22:35:21