Every table of my database has got 2 columns at the end, which allows logging (User who made the action, and Date of the action). EDIT : I use Code-First migrations.
So I would like those two logging columns to be filled automatically :
Each time I insert a new entry in my table (using DbContext.[Model].Add(entry))
OR each time I do a DbContext.SaveChanges() action
I have considered overriding the DbContext.SaveChanges() method, but it didn't work out...
I have also tried overriding the DbSet Add() method, to do the log filling action there. For that I have created a CustomDbSet class which inherits from DbSet :
public class CustomDbSet<TEntity> : DbSet<TEntity> where TEntity : class
{
public TEntity Add(TEntity entity)
{
//Do logging action here
return base.Add(entity);
}
}
But this didn't make it neither.
EDIT : What happens with this CustomDbSet is that any DbContext.[Model] returns null, now (instead of being filled with the content of the database table)
I already have the extension method which will do the logging action, but I don't know where to put it so logging would become an "automatic" action..
public static void EntityLogCreate<T>(this T model, string userName) where T : LogColumns
{
model.Create_User = userName;
model.Create_Date = DateTime.Now;
}
Any idea to achieve that ?