我已经看到了2种类型的实体,例如:
public class Person
{
public int Id {get;set;}
public string Name {get;set;}
public Country Country {get;set;}
}
像这样:
public class Person
{
public int Id {get;set;}
public string Name {get;set;}
public int CountryId {get;set;}
}
我认为第二种方法更轻巧,只有在需要时才可以获取相关数据。
您认为哪个更好?
最佳答案
这取决于您想要什么。如果您只想获取国家/地区的ID,请选择第二个选项。如果您实际上要使用导航属性和/或延迟加载,请选择第一个选项。
就个人而言,我使用实体框架并结合了选项一和两个:
public class Person
{
public int Id {get;set;}
public string Name {get;set;}
public int CountryId {get;set;}
public Country Country {get;set;}
}
因此,从存储库返回数据时,我可以选择。这也意味着,当我要保存时,只需填充实际的值类型属性,而不必加载country对象并将其分配给个人。