如何删除孤立记录
How to Delete Orphaned Records
我正在尝试使用 GraphDiff 和 Entity Framework 更新多个表中的一组记录。一切正常,除了我需要删除任何可能已被替换的拥有实体的孤立记录。我错过了什么,因为我预计这种行为很常见,我只需要弄清楚如何适当地配置上下文或图形。这是我的示例代码:
using (EfDataContext ctx = new EfDataContext())
{
try
{
ctx.Database.Log = msg => _sysLogObject.Debug(msg);
ctx.UpdateGraph(assay, map => map
.OwnedCollection(p => p.Imagings, with => with
.OwnedEntity(p => p.ImagingCellType))
.OwnedEntity(p => p.DisplayTemplate)
.OwnedEntity(p => p.ExportTemplate)
.OwnedEntity(p => p.PrintTemplate)
);
ctx.SaveChanges();
success = true;
}
catch (Exception ex)
{
_sysLogObject.Error(ex);
throw;
}
}
免责声明:我是项目的所有者Entity Framework GraphDiff
我们也通过电子邮件收到了同样的问题。答案是:
The child must have a navigation property toward the parent to make it work. Otherwise, the entity is just skipped by the ChangeTracker from Entity Framework.
我正在尝试使用 GraphDiff 和 Entity Framework 更新多个表中的一组记录。一切正常,除了我需要删除任何可能已被替换的拥有实体的孤立记录。我错过了什么,因为我预计这种行为很常见,我只需要弄清楚如何适当地配置上下文或图形。这是我的示例代码:
using (EfDataContext ctx = new EfDataContext())
{
try
{
ctx.Database.Log = msg => _sysLogObject.Debug(msg);
ctx.UpdateGraph(assay, map => map
.OwnedCollection(p => p.Imagings, with => with
.OwnedEntity(p => p.ImagingCellType))
.OwnedEntity(p => p.DisplayTemplate)
.OwnedEntity(p => p.ExportTemplate)
.OwnedEntity(p => p.PrintTemplate)
);
ctx.SaveChanges();
success = true;
}
catch (Exception ex)
{
_sysLogObject.Error(ex);
throw;
}
}
免责声明:我是项目的所有者Entity Framework GraphDiff
我们也通过电子邮件收到了同样的问题。答案是:
The child must have a navigation property toward the parent to make it work. Otherwise, the entity is just skipped by the ChangeTracker from Entity Framework.