如何使用实体框架更新自引用图?

如何使用实体框架更新自引用图?

问题描述:

我使用实体框架来处理数据库,我也自参照模型如下:如何使用实体框架更新自引用图?

public class PhysicalObject 
{ 
    public PhysicalObject() 
    { 
     SubPhysicalObjects = new HashSet<PhysicalObject>(); 
    } 

    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    public int? ParentId { get; set; } 

    [StringLength(150)] 
    public string Title { get; set; } 

    public virtual PhysicalObject Parent { get; set; } 

    public virtual ICollection<PhysicalObject> SubPhysicalObjects { get; set; } 

} 

我用GraphDiff库更新断开的绘图,但似乎它不支持自我更新引用图。

我的问题是:什么是更新使用实体框架的自我参照图的最佳方式:

  • 删除/更新现有physicalObjects

  • 插入不存在physicalObjects

假设我有两个实体,如下所示:

public class PhysicalObject 
{ 

    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    public int? ParentId { get; set; } 

    public int StorageRequestId { get; set; } 

    public string Title { get; set; } 

    public virtual PhysicalObject Parent { get; set; } 

    public virtual ICollection<PhysicalObject> SubPhysicalObjects { get; set; } 

    public virtual StorageRequest StorageRequest { get; set; } 

} 

public class StorageRequest 
{ 
    public StorageRequest() 
    { 
     PhysicalObjects = new HashSet<PhysicalObject>(); 
    } 

    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    public string Title { get; set; } 

    public virtual ICollection<PhysicalObject> PhysicalObjects { get; set; } 
} 

请注意,PhysicalObject是自引用表。

var oldPhysicalObjects = dbContext.PhysicalObjects.Where(x => x.StorageRequestId== storageRequestId).ToList(); 

var existingIds = new HashSet<int>(); 
foreach (var item in newGraphDto.PhysicalObjects.ToList()) 
    { 
    updateGraph(item, oldPhysicalObjects, dbContext, storageRequestId,existingIds); 
    } 
var posToDelete = oldPhysicalObjects.Where(x => existingIds.All(e => e != x.Id)).ToList(); 
dbContext.PhysicalObjects.RemoveRange(posToDelete); 
dbContext.SaveChanges(); 

updateGraph方法将更新PhysicalObjects递归的每一棵树,它看起来像:

private void updateGraph(PhysicalObjectDto physicalObjectDto, IList<PhysicalObject> oldPhysicalObjects, MyDbContext dbContext, int storageRequestId, HashSet<int> existingIds, PhysicalObject parent = null) 
    { 
     if (physicalObjectAddEditDto.Id == 0) 
     { 
      PhysicalObject po = new PhysicalObject 
      { 
       Id = physicalObjectAddEditDto.Id, 
       Title = physicalObjectAddEditDto.Title, 
       StorageRequestId = storageRequestId, 
       Parent=parent 

      }; 

      dbContext.PhysicalObjects.Add(po); 

      parent = po; 
     } 
     else 
     { 
      var po = oldPhysicalObjects.FirstOrDefault(x => x.Id == physicalObjectAddEditDto.Id); 
      po.Title = physicalObjectAddEditDto.Title; 
      po.StorageRequestId = storageRequestId; 
      po.Parent = parent; 
      dbContext.Entry(po).CurrentValues.SetValues(po); 

      parent = po; 
     } 


     existingIds.Add(parent.Id); 
     foreach (var subPhysicalObject in physicalObjectAddEditDto.SubPhysicalObjects) 
     { 
      updateGraph(subPhysicalObject, oldPhysicalObjects, dbContext, mailRoomRequestId, existingIds, parent); 
     } 

    } 

我希望我的代码将帮助别人知道如何使用实体框架

不是让我们更新图表更新自引用表格的图形树