我有一个继承自activeentity的课程部门
public class ActiveEntity : Entity, IActive
{
public ActiveEntity()
{
IsActive = true;
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid Id { get; set; }
public bool IsActive { get; set; }
[Timestamp, ScaffoldColumn(false), DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Computed)]
public Byte[] Timestamp { get; set; }
[ScaffoldColumn(false)]
public string CreationUserId { get; set; }
[ScaffoldColumn(false)]
public string LastModifiedUserId { get; set; }
}
public class Department:ActiveEntity
{
public Department()
{
this.Address = new DepartmentAddress();
}
[StringLength(9),MinLength(9),MaxLength(9)]
public string Name { get; set; }
public Guid ManagerId { get; set; }
[UIHint("AjaxDropdown")]
public User Manager { get; set; }
public Guid? AddressId { get; set; }
public DepartmentAddress Address { get; set; }
public ICollection<OverheadRate> OverheadRates { get; set; }
}
我只是使用注释没有Fluent API。数据保存到数据Sql Server 2008就好了但是地址对象永远不会被实例化,即使我有上下文使用include
return c.Set<Department>().Include(d => d.Address).Include(d => d.Manager).Where(predicate);
返回数据我运行sql profiler然后运行查询它返回正确的数据。
有任何想法或建议吗?
答案 0 :(得分:0)
删除在this.Address = new DepartmentAddress();
构造函数中实例化地址(Department
)。在默认构造函数中实例化导航引用是邪恶的,并且具有令人讨厌的副作用,如:
EF 4.1 Code First: Why is EF not setting this navigation property?