コード例 #1
0
 private static void Audit(this DomainDbContext context, string user = null)
 {
     foreach (var change in context.ChangeTracker.Entries().Where(x => x.State == EntityState.Added || x.State == EntityState.Modified))
     {
         var readOnly = change.Entity as IReadOnly;
         if (readOnly != null)
         {
             //change.State = EntityState.Unchanged;
             //continue;
             throw new InvalidOperationException("Attempt to change read-only data");
         }
         var audited = change.Entity as IAudited;
         if (audited != null)
         {
             if (change.State == EntityState.Added)
             {
                 audited.Audit.Created = DateTime.Now;
             }
             audited.Audit.Updated = DateTime.Now;
             audited.Audit.User    = user ?? new WindowsPrincipal(WindowsIdentity.GetCurrent()).Identity.Name;
         }
     }
 }
コード例 #2
0
 public SqlCustomerData(DomainDbContext db)
 {
     this.db = db;
 }
コード例 #3
0
 public static async Task <int> SaveAsync(this DomainDbContext context, string user = null)
 {
     context.Audit(user);
     return(await context.SaveChangesWithErrorsAsync());
 }
コード例 #4
0
 public static int Save(this DomainDbContext context, string user = null)
 {
     context.Audit(user);
     return(context.SaveChangesWithErrors());
 }