question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using InboundNavigation in TrackGraph

See original GitHub issue

Using TrackGraph, I would like to be able to set the state of an entity to Added if it is a child in a one-to-many relationship and the parent entity state is Added. For example, if an Order is in an Added state, then I would like to be able to set the OrderDetail to Added as well.

I notice that EntityEntryGraphNode has an InboundNavigation property. Is it possible to use it to find the relationship type and a pointer to the entity on the other side of the relationship?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ajcvickerscommented, Jan 11, 2017

@tonysneed Something like this:

context.ChangeTracker.TrackGraph(blog, e =>
{
    if (e.InboundNavigation != null)
    {
        Debug.Assert(!e.InboundNavigation.IsDependentToPrincipal());

        var dependentEntry = e.Entry;
        var foreignKey = e.InboundNavigation.ForeignKey;

        var keyValues = foreignKey.Properties
            .Select(p => dependentEntry.Property(p.Name).CurrentValue)
            .ToArray();

        var principalEntry =
            dependentEntry.Context.Entry(
                dependentEntry.Context.Find(foreignKey.PrincipalEntityType.ClrType, keyValues));

        // Handle dependent
        dependentEntry.State = principalEntry.State == EntityState.Added
            ? EntityState.Added
            : EntityState.Unchanged;
    }
    else
    {
        // Handle principal
        e.Entry.State = e.Entry.IsKeySet ? EntityState.Unchanged : EntityState.Added;
    }
});

But that only works if the FK is mapped and an the property values are set before calling Add. See #7389.

0reactions
tonysneedcommented, Jan 12, 2017

@ajcvickers Thanks, this is helpful. However, I will need access to the referencing entity, as proposed in #7389, because the FK and parent entity may not have the key set because it is an auto-generated identity column.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EntityEntryGraphNode.InboundNavigation Property
Gets the navigation property that is being traversed to reach this node in the graph.
Read more >
How InboudNavigation works in c# - Stack Overflow
InboundNavigation, this field show me path whick track graph passed to the node I not completely undestand how its work If i have...
Read more >
Change Tracking Mixed-State Graphs in EF Core
Julie says that integration testing is the key to tracking changes in EF Core. Learn what you need to know to get up...
Read more >
ChangeTracker.TrackGraph() in Entity Framework Core
The ChangeTracker.TrackGraph() method begins tracking an entity and any entities that are reachable by traversing it's navigation properties. The specified ...
Read more >
4. Working with Disconnected Entities Including N-Tier ...
Using navigation properties to define relationships​​ You aren't restricted to using the foreign key property to change relationships. You can still use the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found