在实体框架中更新嵌套对象 [英] Updating Nested Objects in Entity Framework
问题描述
最近,我发现EF不会更新嵌套对象. 几天来,我试图弄清楚如何做到这一点,但不幸的是,我仍然坚持这个问题.
recently I discover that EF doesn't update nested object. For few days I try to figure out how to do this but unfortunately I'm stuck with this problem.
我有对象
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; }
}
和排名对象:
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();
}
当有页面时,所有数据都被表示出来,而当我尝试在Service或Position中编辑一些数据时,它不会更新. 有人遇到过这种问题吗?
When have page were all data is represented and when I try to edit some data in Service's or in Position's it doesn't update. Anybody had this kind of problem ?
我看到的每个示例都仅包含嵌套对象,该嵌套对象的深度为1层,但是您可以看到我的对象具有2层嵌套.
Every example that I saw it was only with nested object that has 1 level deep but as you can see my object has 2 level nest.
推荐答案
因此,我弄清楚了如何进行这项工作.
在特定的存储库类中,我做了几个foreach
循环,这些循环将EntityState设置为Modifyed.根据我们的业务规则,需要具有Service和Position的项目以及Position的OrganizationUnit是可选的,因此我检查它是否不为null.
这是我的解决方案:
So I figure out how I can make this work.
In specific repository class I made few foreach
loops that set EntityState to modified. By our business rules Project required to have Service and Position and OrganizationUnit for Position is optional so I check if it's not null.
Here is my solution:
DbContext.Entry(entity).State = EntityState.Modified;
foreach (var service in entity.Service)
{
DbContext.Entry(service).State = EntityState.Modified;
foreach (var position in service.Positions)
{
DbContext.Entry(position).State = EntityState.Modified;
if (position.OrganizationUnit == null) continue;
foreach (var organizationUnit in position.OrganizationUnit)
{
DbContext.Entry(organizationUnit).State = EntityState.Modified;
}
}
}
DbContext.SaveChanges();
现在,当我想在任何级别上更新对象时,它将在数据库中更新.
Now when I want to update my object on any level it will update in my DB.
这篇关于在实体框架中更新嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!