EF Code First IDbSet<TEntity>。Attach() 检测到对关系的角色 _ 的冲突更改 _



我有以下代码:

using (var db = new SourceLogContext())
{
    db.LogEntries.Attach(this);
    db.Entry(this).Collection(c => c.ChangedFiles).Load();
}

我得到以下例外:

System.InvalidOperationException occurred
  Message=Conflicting changes to the role 'LogEntry_LogSubscription_Target' of the relationship 'SourceLog.Model.LogEntry_LogSubscription' have been detected.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.EntityCollection`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.RelatedEnd.WalkObjectGraphToIncludeAllRelatedEntities(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.EntityReference`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
       at System.Data.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
       at System.Data.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
       at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClass2.<Attach>b__1()
       at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
       at System.Data.Entity.DbSet`1.Attach(TEntity entity)
       at SourceLog.Model.LogEntry.LoadChangedFiles() in C:github.comtomhunter-ghSourceLogSourceLog.ModelLogEntry.cs:line 62
  InnerException: (empty)

有问题的线在这里:https://github.com/tomhunter-gh/sourcelog/blob/443fb74a37db895222f85b4dfc941c529292292785e7a/

logentry的logSubscription在此处分配:https://github.com/tomhunter-gh/sourcelog/blob/443fb74a37db8952222f85b45b4dfc941c941c52922922922785e7aa/sourog.sourog.sourcele..mogsel.model.model.model.model/logsub.model/logclogsubl.model/llog codel/llogsubl.model/llog codel/llog codel/llog codel/llog codel/llogsubl.model/llogsubswub

看起来分配创建了一个新的logSubScription实例并分配了它,然后附加引用一个不同的实例(但具有相同的ID)。为什么这会导致错误?我将如何避免呢?

在LogSubscription L88中,我基本上试图将logentry添加到LogSubScription的集合属性中,但没有加载整个Logentry集合。这是正确的方法吗?

通过不附加logentry实体避免错误,而只是查询changefiles集合的数据库:

using (var db = new SourceLogContext())
{
    ChangedFiles = db.LogEntries.Where(x => x.LogEntryId == LogEntryId).Include(x => x.ChangedFiles).Single().ChangedFiles;
}

我实际上在另一种情况下通过覆盖Equals()GetHashCode()

来解决此问题
public override bool Equals(object obj)
{
    var logEntry = obj as LogEntry;
    if (logEntry == null)
        return false;
    return LogEntryId.Equals(logEntry.LogEntryId);
}
public override int GetHashCode()
{
    return LogEntryId.GetHashCode();
}

最新更新