You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Identifying shadow FK values are not propagated when tracked as unchanged or modified #7985
If the shadow PK is of nullable type context.ChangeTracker.TrackGraph(principal, e => e.Entry.State = EntityState.Unchanged);
throws Unable to track an entity of type '...' because primary key property '...' is null
context.Attach(principal) works, but marks the dependent as added
changed the title [-]Identifying shadow FK values are not propagated when trackes as unchanged or modified[/-][+]Identifying shadow FK values are not propagated when tracked as unchanged or modified[/+]on Mar 28, 2017
@AndriySvyryd Having trouble fully understanding this. If the key value of the dependent is null, then changing state to Unchanged without setting the key value should throw. Likewise, using Attach will mark the dependent as Added because it didn't have a key value set, assuming the key is generated. If the key is not generated, then it should throw like TrackGraph does.
@AndriySvyryd When attaching, the primary key must be set. Because when you attach, you are saying that this entity already exists in the database, and hence already has a primary key value assigned.
If the primary key is in shadow state, and you disconnected the entity, then you need to carry the primary key with the entity in some way so that it can then be set again on the state entry before the entity is attached.
Changed the behavior for owned types where the state must match that of the owner, and the key values can be propagated said owner. Behavior for non-owned types remains the same.
@ajcvickers I don't see why owned types would be different, an owned dependent is not required to exist as there's no way of enforcing it.
Carrying around the key values and setting them feels too cumbersome when dealing with an untracked entity graph. In the common cases the dependents will have the same state as the principal, and if not it's simpler to fix the state once the entities are tracked.
I don't think we can say that an existing principal and an existing dependent is any more or less common than an existing principal and an new dependent.
Also, "an owned dependent is not required to exist as there's no way of enforcing it." I think we should discuss this since it seems to be contrary to the concept of an aggregate. That is, it's okay for some entity in an aggregate to not exist, but I don't think it's necessarily okay to then later add it to the aggregate and have it inserted. But if this is really okay, then I think aggregates have the same problem as non-aggregates and I don't see anyway to make Attach work in a standard way for primary keys in shadow state--we simply have no way of knowing the the dependent is new or not and making either choice will be right or wrong in different circumstances.
Marking them as Added would be fine, as long as this works:
context.ChangeTracker.TrackGraph(principal, e => e.Entry.State = EntityState.Unchanged);
We could treat shadow and non-shadow the same if we add API to mark any property as "Set" or "Not Set", this would also allow the users to save CLR default values to the database.
Thinking of both complex type and aggregate scenarios, I think it would be very unfortunate if this throws an exception rather than doing a correct update:
This will work with the PR I sent, but not if the dependent is marked Added. And it doesn't involve the app developer having to know anything about setting special state and/or flags.
Interestingly, this will also work, which is pretty nice:
Customercustomer;using(varcontext=newMyContext()){customer=context.Customers.First();}customer.Address=newAddress{Street="1 Microsoft Way",Zip=98052;};using(varcontext=newMyContext()){context.Update(customer);context.SaveChanges();}
@divega Can you take a look at this today? I'm holding off merging the PR until we decide what the behavior should be for owned dependents, but I think we need to make a decision in time to do something for 2.0.
Activity
[-]Identifying shadow FK values are not propagated when trackes as unchanged or modified[/-][+]Identifying shadow FK values are not propagated when tracked as unchanged or modified[/+]ajcvickers commentedon Jun 26, 2017
@AndriySvyryd Having trouble fully understanding this. If the key value of the dependent is null, then changing state to Unchanged without setting the key value should throw. Likewise, using Attach will mark the dependent as Added because it didn't have a key value set, assuming the key is generated. If the key is not generated, then it should throw like TrackGraph does.
What am I missing?
AndriySvyryd commentedon Jun 27, 2017
@ajcvickers The principal has the key set. How can I attach the principal and the dependent as Unchanged?
ajcvickers commentedon Jun 27, 2017
@AndriySvyryd When attaching, the primary key must be set. Because when you attach, you are saying that this entity already exists in the database, and hence already has a primary key value assigned.
If the primary key is in shadow state, and you disconnected the entity, then you need to carry the primary key with the entity in some way so that it can then be set again on the state entry before the entity is attached.
Make owned entities take their state from owner when attaching
ajcvickers commentedon Jun 27, 2017
Changed the behavior for owned types where the state must match that of the owner, and the key values can be propagated said owner. Behavior for non-owned types remains the same.
AndriySvyryd commentedon Jun 27, 2017
@ajcvickers I don't see why owned types would be different, an owned dependent is not required to exist as there's no way of enforcing it.
Carrying around the key values and setting them feels too cumbersome when dealing with an untracked entity graph. In the common cases the dependents will have the same state as the principal, and if not it's simpler to fix the state once the entities are tracked.
ajcvickers commentedon Jun 27, 2017
I don't think we can say that an existing principal and an existing dependent is any more or less common than an existing principal and an new dependent.
Also, "an owned dependent is not required to exist as there's no way of enforcing it." I think we should discuss this since it seems to be contrary to the concept of an aggregate. That is, it's okay for some entity in an aggregate to not exist, but I don't think it's necessarily okay to then later add it to the aggregate and have it inserted. But if this is really okay, then I think aggregates have the same problem as non-aggregates and I don't see anyway to make Attach work in a standard way for primary keys in shadow state--we simply have no way of knowing the the dependent is new or not and making either choice will be right or wrong in different circumstances.
@divega?
10 remaining items
AndriySvyryd commentedon Jun 27, 2017
Marking them as Added would be fine, as long as this works:
We could treat shadow and non-shadow the same if we add API to mark any property as "Set" or "Not Set", this would also allow the users to save CLR default values to the database.
divega commentedon Jun 28, 2017
@AndriySvyryd @ajcvickers can't wait chat about this in person 😅 Hopefully the three of us will be able to meet in the office soon.
ajcvickers commentedon Jun 28, 2017
Thinking of both complex type and aggregate scenarios, I think it would be very unfortunate if this throws an exception rather than doing a correct update:
Using this model:
This will work with the PR I sent, but not if the dependent is marked Added. And it doesn't involve the app developer having to know anything about setting special state and/or flags.
Interestingly, this will also work, which is pretty nice:
ajcvickers commentedon Jun 29, 2017
@divega Can you take a look at this today? I'm holding off merging the PR until we decide what the behavior should be for owned dependents, but I think we need to make a decision in time to do something for 2.0.
Make entities with unknown keys take their state from owner when atta…
Make entities with unknown keys take their state from owner when atta…