具有相同密钥的对象已经存在objectstatemanager [英] object with same key already exists objectstatemanager

查看:125
本文介绍了具有相同密钥的对象已经存在objectstatemanager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在拥有一个拥有另一个实体的虚拟集合的实体。当我尝试通过填充新插入的对象的虚拟集合插入数据时,它抛出的错误,该对象具有相同的密钥已经存在。



我知道当实体不创建,它将具有0值的身份字段。但是,当我将数据存储在主表中时,我需要存储数据集。

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

这是我正在使用的插入方法。以下是此操作的poco课程(部分实现扩展类以保存数据的收集)。

  public partial 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;组; }
}

public partial class AM_AssetLocations
{
//反向导航
public virtual ICollection< UserDefinedData> UserDefinedDatas {get;组; }
}

我使用json传递数据。哪个也似乎是正确的。因为数据的虚拟集合被正确地添加到实体中。

  {entity:{ID:0 ,CreatedByID:0,CreatedDate:04-13-2014 10:48,ModifiedByID:,ModifiedDate:,DeletedByID Deleted:false,Name:h,Active:true,DisplayOrder:0,Test Decimal:10,:测试编号:10,测试纯文本:h,测试RTF:< p> hsj< / p>,测试是否 :true,TestDate:01-Apr-2014,TestDateTime:10:00 AM,UserDefinedDatas:[{EntityType:AM_AssetLocations,EntityTypeID 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 的},{ENTI tyType : AM_AssetLocations  EntityTypeID: 0, CreatedByID: 0, UserDefinedFieldID: 119, 的valueString: < p为H. 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}]}} 

请帮我解决这个问题。



注意:只要解决这个相同的关键异常,如果我尝试分配身份字段,我的自我就是抛出引用完整性异常。我知道存储实际的收藏应该可以正常工作。但它对我来说并不奏效。请给我一些指导和解决方案。



谢谢,
sachin

解决方案

附加用于附加现有实体。



上下文应该分配适当的状态本身,没有必要在你的情况下手动执行

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

..
< a href =http://msdn.microsoft.com/en-us/data/jj592676.aspx =nofollow> http://msdn.microsoft.com/en-us/data/jj592676.aspx


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

解决方案

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

这篇关于具有相同密钥的对象已经存在objectstatemanager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆