Note that there are some explanatory texts on larger screens.

plurals
  1. POObjectContext.SaveChanges() violates primary key, throws UpdateException?
    primarykey
    data
    text
    <p>Paraphrasing from <a href="http://msdn.microsoft.com/en-us/library/dd283139.aspx" rel="nofollow noreferrer">this MSDN documentation</a> ...</p> <blockquote> <p>An INSERT statement is generated by the Entity Framework and executed on the data source when SaveChanges is called on the ObjectContext. </p> <p>If the INSERT operation succeeds, server-generated values are written back to the ObjectStateEntry. When AcceptChanges is called automatically at the end of the SaveChanges execution, a permanent EntityKey is computed by using the new server-generated values.</p> </blockquote> <p>This does not seem to be occurring in my code. When I call <code>ObjectContext.SaveChanges()</code>, an <code>UpdateException</code> is thrown with <code>InnerException.Message</code> value:</p> <blockquote> <p><strong>"duplicate key value violates unique constraint student_term_data_pkey"</strong></p> </blockquote> <p>Here is the offending code:</p> <pre><code>using (DataAccessLayerEntities context = new DataAccessLayerEntities()) { StudentTermData mostCurrent = new StudentTermData() { // Foreign keys: StudentId = studentId, TermId = currentTerm.Id, // Non-nullable properties: SponsorCode = string.Empty, AdmissionNumber = string.Empty, Expiration = string.Empty }; context.StudentTermDatas.AddObject(mostCurrent); context.SaveChanges(); // UpdateException occurs here. } </code></pre> <p>I have verified that <code>StudentTermData.Id</code> is marked as an EntityKey in my Entity data model. Does anyone have any ideas or suggestions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload