使用 Linq to Entities (EF) 插入多条记录

发布于 2024-08-21 16:27:27 字数 2063 浏览 3 评论 0原文

我有一个映射表(tblUserRoles),它可以包含多个记录。当我使用 L2E 仅插入一条记录时,它会保存,但当尝试保存多条记录时,它会出现异常。我像这样保存:

foreach (tblUserRoles u in InsertUserRole)
{   
     EntityHelperUtil.AddObject(context, "tblUserRoles", (IEntityWithRelationships)u);                            
}

context.SaveChanges();

我正在使用来自(的EntityHelperUtil实用程序类http://bernhardelbl.spaces.live.com/blog/cns!DB54AE2C5D84DB78!238.entry) 这是例外情况:

AttachSingleObject(System.Object, System.Data.Metadata.Edm.EntitySet, System.String)---An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
   at System.Data.Objects.ObjectContext.AttachSingleObject(Object entity, EntitySet entitySet, String argumentName)
   at System.Data.Objects.DataClasses.RelatedEnd.AddEntityToObjectStateManager(IEntityWithRelationships entity, Boolean doAttach)
   at System.Data.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWithRelationships entity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach, HashSet`1 promotedEntityKeyRefs)
   at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity[U](U entity, Boolean addRelationshipAsUnchanged, Boolean doAttach, HashSet`1 promotedEntityKeyRefs)
   at System.Data.Objects.DataClasses.EntityCollection`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach, HashSet`1 promotedEntityKeyRefs)
   at System.Data.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
   at System.Data.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
   at System.Data.Objects.ObjectContext.Attach(IEntityWithKey entity)
   at HealthSphereServices.EntityHelperUtil.AddRelationships(ObjectContext context, List`1 map) in E:\E_Drive data\ORB\ORB_Development\ControlRoom\ControlRoom\HealthSphereServices\EntityHelperUtil.cs:line 158

I have a map table (tblUserRoles) which can contain multiple records. When i insert only one record using L2E it saves but when try to save multiple records it gives exception. I am saving like this:

foreach (tblUserRoles u in InsertUserRole)
{   
     EntityHelperUtil.AddObject(context, "tblUserRoles", (IEntityWithRelationships)u);                            
}

context.SaveChanges();

I am using a utility class of EntityHelperUtil from(http://bernhardelbl.spaces.live.com/blog/cns!DB54AE2C5D84DB78!238.entry)
Here is the exception:

AttachSingleObject(System.Object, System.Data.Metadata.Edm.EntitySet, System.String)---An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
   at System.Data.Objects.ObjectContext.AttachSingleObject(Object entity, EntitySet entitySet, String argumentName)
   at System.Data.Objects.DataClasses.RelatedEnd.AddEntityToObjectStateManager(IEntityWithRelationships entity, Boolean doAttach)
   at System.Data.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWithRelationships entity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach, HashSet`1 promotedEntityKeyRefs)
   at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity[U](U entity, Boolean addRelationshipAsUnchanged, Boolean doAttach, HashSet`1 promotedEntityKeyRefs)
   at System.Data.Objects.DataClasses.EntityCollection`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach, HashSet`1 promotedEntityKeyRefs)
   at System.Data.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
   at System.Data.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
   at System.Data.Objects.ObjectContext.Attach(IEntityWithKey entity)
   at HealthSphereServices.EntityHelperUtil.AddRelationships(ObjectContext context, List`1 map) in E:\E_Drive data\ORB\ORB_Development\ControlRoom\ControlRoom\HealthSphereServices\EntityHelperUtil.cs:line 158

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

給妳壹絲溫柔 2024-08-28 16:27:27

似乎您有:

  1. 重复的客户端生成的 PK 值,或
  2. 服务器生成的 PK,但未在您的 EDMX 中进行映射。因此,EF 不会忽略新对象上的 PK 值。

在没有看到你的模型之前我不能说更多。

Seems like you have either:

  1. A duplicate, client-generated PK value, or
  2. A server-generated PK which is not mapped as such in your EDMX. Hence, the EF doesn't ignore the PK values on the new object.

I can't say more without seeing your model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文