是否可以在EF6中更改实体列表状态
List<AuditTrail> auditLogs = new List<AuditTrail>();
auditLogs = GetLog(context, CreatedBy);
context.AuditTrails.AddRange(auditLogs);
context.Entry(context.AuditTrails).State = System.Data.Entity.EntityState.Unchanged;
它抛出异常。
The entity type DbSet`1 is not part of the model for the current context.
我如何才能达到上述逻辑?
答案 0 :(得分:3)
是。您可以使用linq ForEach为每个元素应用更改状态:
List<AuditTrail> auditLogs = new List<AuditTrail>();
auditLogs = GetLog(context, CreatedBy);
context.AuditTrails.AddRange(auditLogs);
auditLogs.ForEach(l => context.Entry(l).State = System.Data.Entity.EntityState.Unchanged);
请注意,您将状态设置为Unchanged
。你不想把设置状态设置为Changed
吗?