添加,修改或删除子代时如何设置父代的“ modifed_timestamp”?

时间:2019-05-08 13:08:29

标签: c# entity-framework parent-child

如果我仅更改零件对象的值,它就可以正常工作,但是如果删除一个零件对象,则会出现Null指针异常。似乎已删除的零件对象被赋予了状态Entity.Modified(为什么没有Entity.Deleted BTW?)并且缺少指向整体的指针?

var modifiedParts = context.ChangeTracker.Entries<Parts>()
    .Where(x => x.State == EntityState.Modified || x.State == EntityState.Added || x.State == EntityState.Deleted);

foreach (var part in modifiedParts)
{
    part.Entity.Whole.ModifiedTimeStamp = DateTime.Now;
}

0 个答案:

没有答案