Search code examples
c#entity-frameworkexistsobjectstatemanager

object with same key already exists objectstatemanager


I am having an entity which holds the virtual collection of another entity. When i try to insert the data by filling the virtual collection for the newly inserted objects it is throwing the error that object with same key already exists.

I know that when the entity is not created it will have identity field with 0 value. But i need to store the collection of data when i store the data in main table.

public virtual void Insert(TEntity entity)
    {
        ((IObjectState)entity).ObjectState = ObjectState.Added;
        entityDbSet.Attach(entity);
        dataContext.SyncObjectState(entity);
    }

This is the insert method that i am using. and below is the poco classes (partial implementation for extending the classes to hold the collection of data) for this operation.

public partial class UserDefinedData
{
    public int ID { get { return this.UserSelectedDValueID; } set { this.UserSelectedDValueID = value; } }
    public string Name { get { return this.entityTypeName; } }

    public virtual AM_AssetLocations AM_AssetLocations { get; set; }
}

public partial class AM_AssetLocations
{
    // Reverse navigation
    public virtual ICollection<UserDefinedData> UserDefinedDatas { get; set; }               
}

I am passing the data using json. Which is also seems correct. as the virtual collection of data is added to the entity correctly.

{"entity":{"ID":"0","CreatedByID":"0","CreatedDate":"04-13-2014 10:48","ModifiedByID":"","ModifiedDate":"","DeletedByID":"","DeletedDate":"","Deleted":"false","Name":"h","Active":"true","DisplayOrder":"0","Test Decimal":"10","":"","Test Number":"10","Test Plain Text":"h","Test RTF":"<p>hsj</p>","Test Yes No":"false","Test Yes No 2":"true","TestDate":"01-Apr-2014","TestDateTime":"10:00 AM","UserDefinedDatas":[{"EntityType":"AM_AssetLocations","EntityTypeID":"0","CreatedByID":"0","UserDefinedFieldID":"123","ValueNumber":"10"},{"EntityType":"AM_AssetLocations","EntityTypeID":"0","CreatedByID":"0","UserDefinedFieldID":"124","ValueListItemID":"25"},{"EntityType":"AM_AssetLocations","EntityTypeID":"0","CreatedByID":"0","UserDefinedFieldID":"122","ValueNumber":"10"},{"EntityType":"AM_AssetLocations","EntityTypeID":"0","CreatedByID":"0","UserDefinedFieldID":"117","ValueString":"h"},{"EntityType":"AM_AssetLocations","EntityTypeID":"0","CreatedByID":"0","UserDefinedFieldID":"119","ValueString":"<p>hsj</p>"},{"EntityType":"AM_AssetLocations","EntityTypeID":"0","CreatedByID":"0","UserDefinedFieldID":"125","ValueYesNo":0},{"EntityType":"AM_AssetLocations","EntityTypeID":"0","CreatedByID":"0","UserDefinedFieldID":"126","ValueYesNo":1},{"EntityType":"AM_AssetLocations","EntityTypeID":"0","CreatedByID":"0","UserDefinedFieldID":"120","ValueDate":"01-Apr-2014"},{"EntityType":"AM_AssetLocations","EntityTypeID":"0","CreatedByID":"0","UserDefinedFieldID":"121","ValueDate":"08-Apr-2014 10:00 AM"}]}}

Please help me to solve this issue.

Note : Just to solve this same key exception if i try to assign the identity field my self it is throwing referential integrity exception. I know that storing the realative collection should work fine. but it is not working for me. please give me some guidance and solution for this.

Thanks, sachin


Solution

  • Attach is for attaching existing entities.

    Context should assign proper state itself, there's no need to do it manually in your case

    public virtual void Insert(TEntity entity)
    {
        //((IObjectState)entity).ObjectState = ObjectState.Added;
        context.TEntityDbSet.Add(entity);//Add, not Attach!
        //dataContext.SyncObjectState(entity);
        context.SaveChanges()
    }
    

    .. http://msdn.microsoft.com/en-us/data/jj592676.aspx