Skip to content

Identifying shadow FK values are not propagated when tracked as unchanged or modified #7985

Closed

Description

@AndriySvyryd

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

Activity

  1. added this to the 2.0.0 milestone on Mar 27, 2017
  2. 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
  3. modified the milestones: , 2.0.0 on Apr 19, 2017
  4. ajcvickers commented on Jun 26, 2017

    @ajcvickers
    Contributor

    @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?

  5. AndriySvyryd commented on Jun 27, 2017

    @AndriySvyryd
    MemberAuthor

    @ajcvickers The principal has the key set. How can I attach the principal and the dependent as Unchanged?

  6. ajcvickers commented on Jun 27, 2017

    @ajcvickers
    Contributor

    @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.

  7. ajcvickers commented on Jun 27, 2017

    @ajcvickers
    Contributor

    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.

  8. AndriySvyryd commented on Jun 27, 2017

    @AndriySvyryd
    MemberAuthor

    @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.

  9. ajcvickers commented on Jun 27, 2017

    @ajcvickers
    Contributor

    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. 10 remaining items

  11. AndriySvyryd commented on Jun 27, 2017

    @AndriySvyryd
    MemberAuthor

    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.

  12. divega commented on Jun 28, 2017

    @divega
    Contributor

    @AndriySvyryd @ajcvickers can't wait chat about this in person 😅 Hopefully the three of us will be able to meet in the office soon.

  13. ajcvickers commented on Jun 28, 2017

    @ajcvickers
    Contributor

    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:

    Customer customer;
    using (var context = new MyContext())
    {
        customer = context.Customers.First();
    }
    
    customer.Address.CellPhone = "555-555-5555";
    
    using (var context = new MyContext())
    {
        context.Update(customer);
        context.SaveChanges();
    }

    Using this model:

    public class Customer
    {
        public int Id { get; set; }
        public int Name { get; set; }
        public Address Address { get; set; }
    }
    
    public class Address
    {
       public string Street { get; set; }
       public int Zip { get; set; }
       public string HomePhone { get; set; }
       public string CellPhone { get; set; }
    }

    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:

    Customer customer;
    using (var context = new MyContext())
    {
        customer = context.Customers.First();
    }
    
    customer.Address = new Address
    {
        Street = "1 Microsoft Way",
        Zip = 98052;
    };
    
    using (var context = new MyContext())
    {
        context.Update(customer);
        context.SaveChanges();
    }
  14. ajcvickers commented on Jun 29, 2017

    @ajcvickers
    Contributor

    @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.

  15. self-assigned this
    on Jun 30, 2017
  16. removed this from the 2.0.0 milestone on Jun 30, 2017
  17. added this to the 2.0.0 milestone on Jul 3, 2017
  18. removed their assignment
    on Sep 1, 2024
  19. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions