最近我发现EF不会更新嵌套对象。几天来,我一直在尝试解决这个问题,但很遗憾我卡在了这个问题上。
我有一个对象:
然后是服务对象。
当页面上显示所有数据时,当我尝试在服务或职位中编辑某些数据时,它不会更新。有人遇到过这种问题吗?
我看到的每个示例都只有一个嵌套对象,深度为1级,但是如您所见,我的对象有2级嵌套。
我有一个对象:
public class ProjectEntity : AuditableEntity<int>
{
public string CustumerCompany { get; set; }
public string CustomerRepresentative { get; set; }
public string ProjectTitle { get; set; }
public string WwsNumber { get; set; }
[ForeignKey("ParentProjectId")]
public virtual ProjectEntity ParentProject { get; set; }
public int? ParentProjectId { get; set; }
public virtual ICollection<ProjectServicesEntity> Service { get; set; }
}
然后是服务对象。
public class ProjectServicesEntity : AuditableEntity<int>
{
/// <summary>
/// Service Number
/// </summary>
public int Number { get; set; }
/// <summary>
/// Service Name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Positions
/// </summary>
public virtual ICollection<ProjectPositionsEntity> Positions { get; set; }
[ForeignKey("ProjectId")]
public virtual ProjectEntity Project { get; set; }
public int ProjectId { get; set; }
}
和Positions对象:
public class ProjectPositionsEntity : AuditableEntity<int>
{
/// <summary>
/// Position number
/// </summary>
public int Number { get; set; }
/// <summary>
/// Position Name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Organization Unit for position
/// </summary>
public virtual ICollection<ProjectsOutsourcedPositionEntity> OrganizationUnit { get; set; }
/// <summary>
/// Represents if position is outsourced
/// </summary>
public bool OutSource { get; set; }
/// <summary>
/// Comments for position
/// </summary>
public string Remarks { get; set; }
[ForeignKey("ServiceId")]
public virtual ProjectServicesEntity Service { get; set; }
public int ServiceId { get; set; }
}}
我的更新方法:
public void Update(T entity)
{
DbContext.Entry(entity).State = EntityState.Modified;
DbContext.SaveChanges();
}
当页面上显示所有数据时,当我尝试在服务或职位中编辑某些数据时,它不会更新。有人遇到过这种问题吗?
我看到的每个示例都只有一个嵌套对象,深度为1级,但是如您所见,我的对象有2级嵌套。