简体   繁体   English

EF5上的logg / audit跟踪

[英]logg/audit trail on EF5

I am looking for a good way to perform logs change/audit trail on EF5 database first. 我正在寻找一种首先在EF5数据库上执行日志更改/审核跟踪的好方法。

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. 我遇到的主要问题是当前一个旧的应用程序正在崩溃,并且它使用触发器创建了日志,但是在该应用程序上,数据库连接为该应用程序上的每个用户使用一个特定的用户(该应用程序上的每个用户都有自己的数据库用户),因此当他们进行日志记录时,他们会使用很多连接属性作为默认值,例如userID和Host,许多记录的表也没有userID行,因此如果我使用EF,我想更新/插入的实体/ delete没有任何用户数据。

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. 但是我的应用程序(MVC4)仅使用1个用户(每个用户使用相同的数据库用户)进行1个字符串连接,因此触发器将存储连接字符串中数据库用户的userId。

so what will be a good way to create logs using EF? 那么使用EF创建日志的好方法是什么? is there a way to do it using triggers?(and passing userID and others?). 有没有一种方法可以使用触发器?(并传递userID和其他?)。

i have being reading about override onUpdate functions but also they say it wont work on EF5 我正在阅读有关重写onUpdate函数的信息,但他们也说它不能在EF5上运行

In the DatabaseContext it is possible to override the SaveChanges function. 在DatabaseContext中,可以覆盖SaveChanges函数。

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. 也许是底层的,即靠近数据层,但是它将在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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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