NickStrupat / EntityFramework.Triggers

Adds events for entity inserting, inserted, updating, updated, deleting, and deleted
MIT License
373 stars 46 forks source link

How to user this EntityFramework.Triggers with Repository pattern #43

Closed umar-ulabs closed 4 years ago

umar-ulabs commented 5 years ago

I am having hard time integration this library in my app, it has UnitOfWork and Repository pattern. I also want to pass logged in user info who performed the action from asp.net core to data layer. It seems this library can easily solve a complex issue in my app where I am logging in each controller. Thank you

NickStrupat commented 5 years ago

I'm guessing you're looking for something like this...

Triggers<Entity>.GlobalInserting.Add<IHttpContextAccessor>(e => {
    var user = e.Service.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
});
UmarLone commented 5 years ago

Should the above code in each entity class or in SaveChanges that exposed by Data Layer?. Thanks

NickStrupat commented 5 years ago

The code above should go wherever you want to initialize the triggers (generally you always want to do this once at start-up along with your other scaffolding).

It all depends when and where you want that trigger to be fired upon SaveChanges()