locked
The EntityKey property can only be set when the current value of the property is null RRS feed

  • Question

  • Hello All!

    I am using Compact SQL 3.5 with EntityFramework and .Net 4.0.  I create an object, set all properties and attempt to add it to the associated table and receive the error in the title above.  I have no clue what or why this message is related to or showing up.  Below is a touch of code that shows the "context" of when this message shows up.

     

     

     

    /// <summary>

     

     

    /// Punch-in Punch-out, the timeclock object.

     

     

    /// </summary>

     

     

    public partial class TIMEPUNCH

    {

    #region

     

     

    Public Static Functions

     

     

    public static void AddTimePunch(TIMEPUNCH tp)

    {

     

     

    using (CompactDominicaEntities objectContext = new CompactDominicaEntities(CompactDominicaEntities.GetLocalEfConnectionString()))

    {

    objectContext.TIMEPUNCHES.AddObject(tp);

    objectContext.SaveChanges();

    }

    }

    #endregion

     

     

    Public Static Functions

    }

    • Moved by VMazur Monday, September 27, 2010 10:31 AM (From:ADO.NET Managed Providers)
    Saturday, September 25, 2010 9:15 PM

Answers

  • Update:  It appears that when I create the new TIMEPUNCH object a temporary EntityKey is created and the objects state is already set at Added.  I want to simply persist this object to the database but when creating a context that points to the database, all blows up since it thinks it is already added.  But what is it added to?  I would detach the object that thinks it is added, but I don't know what to detach it from since there was no context explicitly created in the first place.
    Saturday, September 25, 2010 10:56 PM