title: Entity Framework DbUpdateException when storing m:n relationship
body: <p>I'm having troubles with creating and/or storing m:n relationship with EF 4.3 Code first</p>
  
  <p>So the first entity <code>Publication</code> is defined as with some other internal scalar properties:</p>
  
  <pre><code>    public class Publication : IDataErrorInfo{
  
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int PublicationId { get; set; }
  
      [InverseProperty("Publications")]
      public virtual ICollection&lt;Group&gt; Groups { get; set; }
  </code></pre>
  
  <p>and the other class in the same way:</p>
  
  <pre><code>    public class Group : IDataErrorInfo {
  
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int GroupId { get; set; }
  
      [InverseProperty("Groups")]
      public ICollection&lt;Publication&gt; Publications { get; set; }
  </code></pre>
  
  <p>which according to numerous articles should be fine.</p>
  
  <p>There are several problems I occur. AT first:</p>
  
  <ul>
  <li>If I create a new publication and assert it some Groups. All is stored to the db. But then I restart the program, the same particular publication has ICollection set to null. Therefore the information about relationship with Group has been deleted.I don't know why :(</li>
  <li>When I try to update exisiting Publication entry with Group relationship, the DBUpdateException is thrown with the following text: </li>
  </ul>
  
  <blockquote>
    <p>An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types</p>
  </blockquote>
  
  <p>In the inner exception is the same, and in the inner exception of this one is the following: </p>
  
  <blockquote>
    <p>Violation of PRIMARY KEY constraint 'PK_<em>Publicat</em>_3AF5D6A10AD2A005'. Cannot insert duplicate key in object 'dbo.PublicationGroups'.
    The statement has been terminated.</p>
  </blockquote>
  
  <p>I'm asserting the the new values of the Publication as follows:</p>
  
  <pre><code>var entry = db.Publications.First(a =&gt; a.PublicationId == publKey);
  entry.Groups = db.Groups.
                  Where(a =&gt; groupKeys.Contains(a.GroupId)).
                  Select(b =&gt; b).
                  ToList();
  </code></pre>
  
  <p>where <code>publKey</code> is the key of the edited entity and <code>groupKeys</code> is the List of GroupId which should be the publication be related to.</p>
  
  <p>after calling <code>db.SaveContext()</code> the exception is thrown</p>
  
  <p>This topic has been covered by numerous articles, but I didn't find any solution. All of the examples are using the same code, but apparently I'm missing something. I'm using SQL Ce 4.0 as the persistance data storage.</p>
  
  <p>Thank you guys for answer, I'm dealing with it since yesterday, but don't why this happens</p>