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();
}
}
Technically this is not a problem, since you can easily create a connection to another database (or even open multiple connections to the same database), which is what the
DbContextis doing under the covers.