hi,
I have 3 tables:
- Activity
- ACT_Activity
- ActivityToEntity
All table have a PK named ID
the PKs of Activity and ActivityToEntity are defined Auto Generate in DB
the relation betweeen Activity and ACT_Activity is one to one
by Activity.ID(PK) and ACT_Activity.ID(PK)
the relation betweeen Activity and ActivityToEntity is one to many
by Activity.ID(PK) and ActivityToEntity .ActivityID(FK)
I select from DB Activity, add 2 ActivityToEntity and save and all is well
but if I select Activity and load ACT_Activity
like that:
context.Entry(activity1).Reference(a => a.ACT_Activity).Load();
add 2 ActivityToEntity and save
I get the following error:
in TrackableEntities.EF6. DbContextExtensions in function: SetEntityState at line:
context.Entry(item).State = state;
Attaching an entity of type 'TrackableGettingStarted.Entities.Shared.Net45.ActivityToEntity' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
if I add one ActivityToEntity all is good again
What do I need to change so I can add 2 ActivityToEntity?
Thanks in advance!
hi,
I have 3 tables:
All table have a PK named ID
the PKs of Activity and ActivityToEntity are defined Auto Generate in DB
the relation betweeen Activity and ACT_Activity is one to one
by Activity.ID(PK) and ACT_Activity.ID(PK)
the relation betweeen Activity and ActivityToEntity is one to many
by Activity.ID(PK) and ActivityToEntity .ActivityID(FK)
I select from DB Activity, add 2 ActivityToEntity and save and all is well
but if I select Activity and load ACT_Activity
like that:
context.Entry(activity1).Reference(a => a.ACT_Activity).Load();
add 2 ActivityToEntity and save
I get the following error:
in TrackableEntities.EF6. DbContextExtensions in function: SetEntityState at line:
context.Entry(item).State = state;
Attaching an entity of type 'TrackableGettingStarted.Entities.Shared.Net45.ActivityToEntity' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
if I add one ActivityToEntity all is good again
What do I need to change so I can add 2 ActivityToEntity?
Thanks in advance!