我正在使用Entity Framework 4.1(代码优先)开展一个小型示例项目.我的课程看起来像这样:
public class Context : DbContext
{
public IDbSet<Person> People { get; set; }
public IDbSet<EmployeeType> EmployeeTypes { get; set; }
}
public class Person
{
[Key]
public int Key { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
virtual public EmployeeType EmployeeType { get; set; }
}
public class EmployeeType
{
[Key]
public int Key { get; set; }
public string Text { get; set; }
virtual public …
Run Code Online (Sandbox Code Playgroud) 我首先使用代码,可以毫无问题地添加记录.正确创建数据库并且没有任何问题地播种.但是在我的编辑操作中调用SaveChanges()只会更新实体,而不会更新任何导航或引用属性.
我的模型的简化版本:
public class Contact : IMyBaseObject
{
public Contact()
{
this.Delete = false;
this.ContactTypes = new HashSet<ContactType>();
}
public int Id {get; set;}
public string Name {get;set;}
public bool Delete {get;set;}
public virtual ICollection<ContactType> ContactTypes { get; set; }
public virtual USState USState { get; set; }
}
public class ContactType : MyBaseObject
{
public ContactType()
{
}
public int Id {get; set;}
public string Name {get;set;}
public virtual ICollection<Contact> Contacts {get;set;}
}
public abstract class Territory : MyBaseObject …
Run Code Online (Sandbox Code Playgroud)