English 中文(简体)
Insert multiple records using Linq to Entities (EF)
原标题:

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 dataORBORB_DevelopmentControlRoomControlRoomHealthSphereServicesEntityHelperUtil.cs:line 158
问题回答

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.





相关问题
LINQ Training: Native SQL Queries to LINQ

Can anyone point out some good LINQ training resources. Mostly interested in getting some developers that are very skilled with SQL up to speed using Lambda and LINQ queries. They are struggling with ...

How can i write Linq2Entities Query with inner joins

How can I get data from these related entities. I want to get these columns only: Term.Name , related Concept_Term.Weight, related Concept.Id I wrote the SQL but I don t want to use select t....

Linq to enties, insert foreign keys

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys. this is the code that i am using, I would appreciate any ...

Linq to entity timing problem

I created a data model that has some views from my database as entities,when I create a linq statment that has part of this view s entities ,the time processor take to execute the linq statment is ...

热门标签