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.

Subscription to ChangeTracker.StateChanged events

See original GitHub issue

Can you please advise how to subscribe to ChangeTracker.StateChanged events but keep reverse engineering automatic-generated DbContext file intact?

This works in the original file:

public partial class MyContext : DbContext
{
    public MyContext(DbContextOptions<MyContext> options)
        : base(options)
    {
        ChangeTracker.StateChanged += OnEntityStateChanged;
    }

This doesn’t work in a separate file:

public partial class MyContext
{
    public MyContext() : base()
    {
        ChangeTracker.StateChanged += OnEntityStateChanged;
    }

Or maybe you can advise a way how to subscribe to events outside from default DbContext constructor. So little information how to use it, and most SO posts suggest to put subscription into default constructor.

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ErikEJcommented, Mar 29, 2023

Then you can use the t4 template to modify the constructor

0reactions
ErikEJcommented, Mar 30, 2023

Cool, let me know if you need additional advice

Read more comments on GitHub >

github_iconTop Results From Across the Web

ChangeTracker.StateChanged Event
An event fired when an entity that is tracked by the associated DbContext has moved from one EntityState to another.
Read more >
ChangeTracker.StateChanged of DbContext gets triggered ...
I found that ChangeTracker.StateChanged is triggered when _dbContext.Entry(student) is called. Then it doesn't get triggered again when ...
Read more >
Change Detection and Notifications - EF Core
EF Core fires the ChangeTracker.Tracked event when an entity is tracked for the first time. Future entity state changes result in ChangeTracker.
Read more >
How to Use .NET Evens provided by EF Core
ChangeTracker.StateChanged – This event is raised when an operation is performed on an entity, that caused entity to change its EntityState ...
Read more >
5 Ways to Trigger Actions Before or After Save in EF
You can subscribe to these events and provide a delegate that handles them. For example, you can use the StateChanged event to check...
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