Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1055817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:37:17+00:00 2026-05-16T17:37:17+00:00

Paraphrasing from this MSDN documentation … An INSERT statement is generated by the Entity

  • 0

Paraphrasing from this MSDN documentation …

An INSERT statement is generated by
the Entity Framework and executed on
the data source when SaveChanges is
called on the ObjectContext.

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.

This does not seem to be occurring in my code. When I call ObjectContext.SaveChanges(), an UpdateException is thrown with InnerException.Message value:

“duplicate key value violates unique constraint student_term_data_pkey”

Here is the offending 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.
}

I have verified that StudentTermData.Id is marked as an EntityKey in my Entity data model. Does anyone have any ideas or suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:37:18+00:00Added an answer on May 16, 2026 at 5:37 pm

    This problem was caused by a bug in EF4 where the EF designer doesn’t set the StoreGeneratedPattern attribute in the SSDL. The problem is documented in this blog and this Microsoft Connect ticket.

    The solution was to open my model’s .edml file in a text editor, locate the <EntityType Name="student_term_data"> XML tag, and add StoreGeneratedPattern="Identity" to the property tag being used as an EntityKey:

      <EntityType Name="student_term_data">
         <Key><PropertyRef Name="id" /></Key>
         <Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
         ...
      </EntityType>
    

    I then re-opened my model in the EF designer, made a small change (moved an entity position) and saved. This caused the code generator to run, presumably synchronizing the CDSL with the SSDL.

    My code now runs as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.