简体   繁体   中英

logg/audit trail on EF5

I am looking for a good way to perform logs change/audit trail on EF5 database first.

The main problem i'm having is that currently an old application is ruining and it creates logs using Triggers, but on that application the database connection uses a specific user for each user on the application (every user on the application has his own database user), so when they do a log they use a lot of the connection properties as default values like userID, and Host, also many logged tables doesn't have an userID row so if i use EF, the entity i want to update/insert/delete doesn't have any user data.

but my application (MVC4) has only 1 string connection using only 1 user (same database user for each) so the triggers will store the userId of the database user from the connection string.

so what will be a good way to create logs using EF? is there a way to do it using triggers?(and passing userID and others?).

i have being reading about override onUpdate functions but also they say it wont work on EF5

In the DatabaseContext it is possible to override the SaveChanges function.

You can test the changeset for entries that needs to be logged. Maybe it's to low-level ie to close to the datalayer, but it will work in EF.

You'll get something like this:

public override int SaveChanges()
{
    foreach (var entry in ChangeTracker.Entries())
    {
        if (entry.State == EntityState.Added)
        {
            var needToLogAdd = entry.Entity as INeedToLogAdd;
            if (needToLogAdd != null)
                    DoLogAdd(needToLogAdd);
        }
    }

    base.SaveChanges();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM