简体   繁体   中英

Entity Framework context in context

i have a context class like below,i want to log all db operation where another database,using a DbContext in another DbContext?is it a problem?

public class MyContext : DbContext
{
    public DbSet<SiteUser> SiteUsers { get; set; }
    public DbSet<SystemLanguage> SystemLanguages { get; set; }

    public int SaveChanges(string userId)
    {
        LogContext logDB = new LogContext();

        var entries = this.ChangeTracker.Entries()
            .Where(p => p.State == EntityState.Added || 
                p.State == EntityState.Deleted || 
                p.State == EntityState.Modified);

        foreach (var entry in entries)
        {
            foreach (AuditLog log in
                GetAuditRecordsForChange(entry, userId))
            {
                logDB.AuditLogs.Add(log);
            }
        }

        logDB.SaveChanges();

        return base.SaveChanges();
    }
}

从技术上讲,这不是问题,因为您可以轻松地创建到另一个数据库的连接(甚至可以打开到同一数据库的多个连接),而这正是DbContextDbContext所做的。

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