We have worked on a project for 1 month and there are 6 entities without any relationship with other entities. They are all simple entities.
We have created 6 diffe
To 'override' SaveChanges without needing to create all that extra code, I wrapped it in its own extension method. i.e.
public static int SaveChangesWrapped(this MyDbContext db)
{
// do other things
return db.SaveChanges();
}
or async...
public static Task SaveChangesAsyncWrapped(this MyDbContext db)
{
// do other things
return await db.SaveChangesAsync();
}
You can tweak it however you need, i.e. add other parameters, make it generic, etc. I may be missing something but this was enough for me to 'override' SaveChanges and cut down a whole lot of repeated code.
Then anywhere in your code when you want to save, it's just:
db.SaveChangesWrapped();
//or
await db.SaveChangedAsyncWrapped();