Question

I have the following code:

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

And I'm getting the following exception:

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.com\tomhunter-gh\SourceLog\SourceLog.Model\LogEntry.cs:line 62
  InnerException: (empty)

The offending line is here: https://github.com/tomhunter-gh/SourceLog/blob/443fb74a37db89522f85b4dfc941c52922785e7a/SourceLog.Model/LogEntry.cs#L62

The LogEntry has it's LogSubscription assigned here: https://github.com/tomhunter-gh/SourceLog/blob/443fb74a37db89522f85b4dfc941c52922785e7a/SourceLog.Model/LogSubscription.cs#L88

It looks like the assignment creates a new LogSubscription instance and assigns it, and then the Attach references a different instance (but with the same Id). Why does this cause the error and how would I avoid it?

In LogSubscription L88 I'm basically trying to add the LogEntry to the collection property of the LogSubscription but without loading the entire LogEntry collection. Is this the correct way to do that?

Was it helpful?

Solution

Avoiding the error by not attaching the LogEntry entity, and just querying the database for the ChangedFiles collection instead:

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

OTHER TIPS

I've actually solved this in another situation by overriding Equals() and 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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top