title: EF Code First IDbSet<TEntity>.Attach() Conflicting changes to the role _ of the relationship _ have been detected
body: <p>I have the following code:</p>
  
  <pre><code>using (var db = new SourceLogContext())
  {
      db.LogEntries.Attach(this);
      db.Entry(this).Collection(c =&gt; c.ChangedFiles).Load();
  }
  </code></pre>
  
  <p>And I'm getting the following exception:</p>
  
  <pre><code>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.&lt;&gt;c__DisplayClass2.&lt;Attach&gt;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)
  </code></pre>
  
  <p>The offending line is here: <a href="https://github.com/tomhunter-gh/SourceLog/blob/443fb74a37db89522f85b4dfc941c52922785e7a/SourceLog.Model/LogEntry.cs#L62" rel="nofollow">https://github.com/tomhunter-gh/SourceLog/blob/443fb74a37db89522f85b4dfc941c52922785e7a/SourceLog.Model/LogEntry.cs#L62</a></p>
  
  <p>The LogEntry has it's LogSubscription assigned here: <a href="https://github.com/tomhunter-gh/SourceLog/blob/443fb74a37db89522f85b4dfc941c52922785e7a/SourceLog.Model/LogSubscription.cs#L88" rel="nofollow">https://github.com/tomhunter-gh/SourceLog/blob/443fb74a37db89522f85b4dfc941c52922785e7a/SourceLog.Model/LogSubscription.cs#L88</a></p>
  
  <p>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?</p>
  
  <p>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?</p>