Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One to one issue (add 2 child) #235

Open
orit1 opened this issue Nov 7, 2019 · 4 comments
Open

One to one issue (add 2 child) #235

orit1 opened this issue Nov 7, 2019 · 4 comments

Comments

@orit1
Copy link

orit1 commented Nov 7, 2019

hi,

I have 3 tables:

  1. Activity
  2. ACT_Activity
  3. 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!

@tonysneed
Copy link
Collaborator

tonysneed commented Nov 7, 2019 via email

@orit1
Copy link
Author

orit1 commented Nov 7, 2019

it's already in Added TrackingState

@orit1
Copy link
Author

orit1 commented Nov 7, 2019

the error appears when entity is ACT_Activity
before it cames to the ActivityToEntity child
at line:
context.Entry(item).State = state;

@orit1
Copy link
Author

orit1 commented Nov 7, 2019

if I change the entity framework state for child (ActivityToEntity) before ApplyChanges()
that like:
context.Entry(activity.ActivityToEntity[0]).State = EntityState.Added;
context.Entry(activity.ActivityToEntity[1]).State = EntityState.Added;
all is good

so I think that the problem is that when I entry first time to set ACT_activity state I already have 2 child in Activity object and they both without entity framework state Added (deAttach)
Even though TrackingState is Added but is not relevant for entity framework

It is a normal behavior and I do something worng or it is a something that need to be fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants