我从ReSharper收到一条关于从我的对象构造函数调用虚拟成员的警告.
为什么不做这件事?
我曾经看过一些书(例如编程实体框架代码Julia Lerman)定义了他们的域类(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; }
}
Run Code Online (Sandbox Code Playgroud)
生成POCO时,其他一些书籍或工具(例如Entity Framework Power Tools)初始化类的导航属性,如:
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 { …Run Code Online (Sandbox Code Playgroud) c# domain-driven-design entity-framework navigation-properties ef-code-first
鉴于以下代码,EF/DbContext如何了解对客户对象所做的更改:
class Program
{
static void Main()
{
using(var shopContext = new ShopContext())
{
var customer = shopContext.Customers.Find(7);
customer.City = "Marion";
customer.State = "Indiana";
shopContext.SaveChanges();
}
}
}
public class ShopContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string State { …Run Code Online (Sandbox Code Playgroud)