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

  • SEARCH
  • Home
  • 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 6356177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:54:52+00:00 2026-05-24T22:54:52+00:00

I have managed to successfully clone an EF entity using serialization and deserialization. If

  • 0

I have managed to successfully clone an EF entity using serialization and deserialization. If I set the EntityKey to nothing, I can add it to the context. But when I try to SaveChanges, I get an error saying that the primary key must be unique. This makes sense since the clone has the same key. So I need to change it beforehand.

But the primary key is autoassigned by the DB (SQLite) upon insertion, and since the PK is not nullable I cannot set NewEntity.ID=Nothing, which I presume would tell the context that this entity should receive a temporary key until it is inserted.

If I set NewEntity.ID = 30804328 or some arbitrary (unused) number, it will save to DB fine. But I am very unkeen to query for an unused ID value every time I want to clone an entity.

I understood that the context would treat a detached entity as new when it was ‘AddObject’ed, and assign it a temporary key so the DB could do the assignment and then the context would receive the updates. Is this not the case?

How do I resolve this? Thanks for any advice!

  • 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-24T22:54:53+00:00Added an answer on May 24, 2026 at 10:54 pm

    I FOUND THE SOLUTION FINALLY. The problem was that the table involved had a primary key identity but Autoincrement was not set. It appears that though the ID key is zero by default when creating a new entity, and it cannot be changed by assignment, it is auto-assigned in the case that Autoincrement is set. But clearly this cannot be resolved automatically if autoincrement (or computed) is not set.

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

Sidebar

Related Questions

I have successfully managed to get System.Speech.Synthesis to read English text in arbitrary voices
Have anyone successfully managed to setup a combined Java/C++ project for Eclipse? What I
I have successfully implemented interop beftween Win32 application and managed .Net dll as described
After much fiddling, I've managed to install the right ODBC driver and have successfully
I have managed to set up Cassandra + Thrift and the Python wrapper for
I have managed to create a custom action in C# using MakeSfxCA which is
If I have managed to locate and verify the existence of a file using
I've managed to plug away through the YouTube documentation, and have managed to successfully
I have successfully managed to bind images stored in Isolated Storage to Image elements
Hi guys I am rather new at AJAX programming but I have managed to

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.