EFCodeFirst:我應(yīng)該初始化導(dǎo)航屬性嗎?我看過一些書編程實(shí)體框架代碼首先Julia Lerman)在沒有初始化導(dǎo)航屬性的情況下定義它們的域類(Poco),如下所示:public class User{
public int Id { get; set; }
public string UserName { get; set; }
public virtual ICollection<Address> Address { get; set; }
public virtual License License { get; set; }}其他一些書籍或工具(例如實(shí)體框架電動(dòng)工具)當(dāng)生成pocos初始化類的導(dǎo)航屬性時(shí),如下所示:public class User{
public User()
{
this.Addresses = new IList<Address>();
this.License = new License();
}
public int Id { get; set; }
public string UserName { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual License License { get; set; }}問題1:哪個(gè)更好?為什么?贊成與反對(duì)?編輯:public class License{
public License()
{
this.User = new User();
}
public int Id { get; set; }
public string Key { get; set; }
public DateTime Expirtion { get; set; }
public virtual User User { get; set; }}問題2:在第二種方法中,如果“License”類也引用了“user”類,則會(huì)出現(xiàn)堆棧溢出。這意味著我們應(yīng)該有單向的參考。(?)我們應(yīng)該如何決定應(yīng)該刪除哪個(gè)導(dǎo)航屬性?
EFCodeFirst:我應(yīng)該初始化導(dǎo)航屬性嗎?
慕運(yùn)維8079593
2019-07-09 14:29:58