Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
How do I target another database with tracker-enabled-dbcontext

I'm trying to implement the tracker-enabled-dbcontext package from the documentaion and tracker-enabled-dbcontext git repository But I'm unable to change save changes to target a different database. I've modified my SaveChanges

public class MyDBContext : DbContext, IUnitOfWork {}
public class MacsAuditDbContext : TrackerEnabledDbContext.TrackerContext {}

in MyDBContext

public override int SaveChanges()
        {
            DateTime nowAuditDate = DateTime.Now;
            IEnumerable<System.Data.Entity.Infrastructure.DbEntityEntry<DomainEntity>> changeSet = ChangeTracker.Entries<DomainEntity>();
            if (changeSet != null)
            {
                foreach (System.Data.Entity.Infrastructure.DbEntityEntry<DomainEntity> entry in changeSet)
                {
                    switch (entry.State)
                    {
                        case EntityState.Added:
                            entry.Entity.Created = nowAuditDate;
                            entry.Entity.Modified = nowAuditDate;
                            break;
                        case EntityState.Modified:
                            entry.Entity.Modified = nowAuditDate;
                            break;
                    }
                }
            }

            using (MacsAuditDbContext db = new MacsAuditDbContext())
            {
                db.SaveChanges();

            }

            return base.SaveChanges();
        }

in my startup class

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            AuthConfig.Register(app);
            GlobalConfiguration.Configuration
    .UseSqlServerStorage("MacsAuditDbContext");
                }
    }

But I am still unable to save audit logs to the target(secondary) database. my Domain entires saving my primary DB but not audit logs. Do I want to pass MyDBContext to MacsAuditDbContext? Or Am I doing something wrong? please help me.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can try leveraging OnAuditLogGenerated event. Something along this lines:

public sealed class MyDBContext : TrackerContext
{
    public MyDBContext ()
    {
        OnAuditLogGenerated += SaveToAnotherDb;
    }

    private void SaveToAnotherDb(object? sender, AuditLogGeneratedEventArgs args)
    {
        var auditLog = args.Log;

        using (MacsAuditDbContext db = new MacsAuditDbContext())
        {
            db.AuditLog.Add(auditLog);
            db.SaveChanges();
        }

        //skips saving to local database
        args.SkipSavingLog = true;
    }

    protected override void Dispose(bool disposing)
    {
        OnAuditLogGenerated -= SaveToAnotherDb;
        base.Dispose(disposing);
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda