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 6939689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:39:15+00:00 2026-05-27T12:39:15+00:00

I create a new object and want to attach it to a context like

  • 0

I create a new object and want to attach it to a context like this,

User user = new User();
user.userName=”Kobe”;
context.Attach(user);

An error message appears – “An object with a null EntityKey value cannot be attached to an object context”.
If I query out a user object from database and assign it’s EntityKey to the new object, then detach the query result object like this,

User user = (from u in context.Users where u.userID == 1 select u).First();
User newUser = new User();
newUser.userName = “Kobe”;
newUser.EntityKey = user.EntityKey;
context.Detach(user);
context.Attach(newUser);

Another error message appears – “The object cannot be attached because the value of a property that is a part of the EntityKey does not match the corresponding value in the EntityKey.”
I really don’t know what the EntityKey is, I searched on the internet and have seen the EntityKey Class in MSDN, but still can’t understand clearly. When the EntityKey created and attached to an object? where can I find it? If I detach the object, why the EntityKey is still exist?

Anyone can help? Thanks in advance!

  • 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-27T12:39:16+00:00Added an answer on May 27, 2026 at 12:39 pm

    An EntityKey is an object that is used by the Entity Framework to uniquely identify your object and keep track of it.

    When you construct a new object the key properties of your entity are null (or 0). The ObjectContext doesn’t know that you’re entity exists and doesn’t track it yet so there is no entity key.

    When you add the object to the context, a temporary key is constructed.
    After that you can save the changes to your database. This will generate an Insert statement and retrieve the new key from the database, construct the permanent EntityKey and update all the references it has to the temporary key.

    Attaching is another story. You attach an entity to the ObjectContext when the object already exists in your database but has no connection to an ObjectContext.

    So in your case you should change your code for adding a new entity to:

    User user = new User();
    user.userName=”Kobe”;
    
    context.Users.Add(user); // Generate a temporary EntityKey
    // Insert other objects or make changes
    context.SaveChanges(); // Generate an insert statement and update the EntityKey
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP, to create a new object you would do something like this, $dog
I want to create a new object using Class::DBI. One of the fields of
I want code to run whenever I create a new object. For example, see
Consider this bit of obfuscated code. The intention is to create a new object
This is probably a simple matter, but when I create a new object, the
I am tryıng to create new object of other class ın a for loop.
I'm trying to create a new object of type T via its constructor when
In my example, I'm trying to create a new object which has references to
I create a new Button object but did not specify the command option upon
When I create a new Date object, it is initialized to the current time

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.