Esempio n. 1
0
        public void SaveChangesFailed(DbContextErrorEventData eventData)
        {
            using (var auditContext = new AuditContext(_connectionString))
            {
                auditContext.Attach(_audit);
                _audit.Succeeded    = false;
                _audit.EndTime      = DateTime.UtcNow;
                _audit.ErrorMessage = eventData.Exception.Message;

                auditContext.SaveChanges();
            }
        }
Esempio n. 2
0
        public int SavedChanges(SaveChangesCompletedEventData eventData, int result)
        {
            using (var auditContext = new AuditContext(_connectionString))
            {
                auditContext.Attach(_audit);
                _audit.Succeeded = true;
                _audit.EndTime   = DateTime.UtcNow;

                auditContext.SaveChanges();
            }

            return(result);
        }
Esempio n. 3
0
        public async Task SaveChangesFailedAsync(
            DbContextErrorEventData eventData,
            CancellationToken cancellationToken = default)
        {
            using (var auditContext = new AuditContext(_connectionString))
            {
                auditContext.Attach(_audit);
                _audit.Succeeded    = false;
                _audit.EndTime      = DateTime.UtcNow;
                _audit.ErrorMessage = eventData.Exception.InnerException?.Message;

                await auditContext.SaveChangesAsync(cancellationToken);
            }
        }
Esempio n. 4
0
        public InterceptionResult <int> SavingChanges(
            DbContextEventData eventData,
            InterceptionResult <int> result)
        {
            _audit = CreateAudit(eventData.Context);

            using (var auditContext = new AuditContext(_connectionString))
            {
                auditContext.Add(_audit);
                auditContext.SaveChanges();
            }

            return(result);
        }
Esempio n. 5
0
        public async ValueTask <InterceptionResult <int> > SavingChangesAsync(
            DbContextEventData eventData,
            InterceptionResult <int> result,
            CancellationToken cancellationToken = default)
        {
            _audit = CreateAudit(eventData.Context);

            using (var auditContext = new AuditContext(_connectionString))
            {
                auditContext.Add(_audit);
                await auditContext.SaveChangesAsync();
            }

            return(result);
        }
Esempio n. 6
0
        public async ValueTask <int> SavedChangesAsync(
            SaveChangesCompletedEventData eventData,
            int result,
            CancellationToken cancellationToken = default)
        {
            using (var auditContext = new AuditContext(_connectionString))
            {
                auditContext.Attach(_audit);
                _audit.Succeeded = true;
                _audit.EndTime   = DateTime.UtcNow;

                await auditContext.SaveChangesAsync(cancellationToken);
            }

            return(result);
        }
Esempio n. 7
0
        private static async Task CreateDatabases()
        {
            using (var context =
                       new AuditContext(@"Server=(localdb)\mssqllocaldb;Database=Demo5.Audit;Trusted_Connection=True;ConnectRetryCount=0"))
            {
                await context.Database.EnsureDeletedAsync();

                await context.Database.EnsureCreatedAsync();
            }

            using (var context = new BlogsContext())
            {
                await context.Database.EnsureDeletedAsync();

                await context.Database.EnsureCreatedAsync();
            }
        }
Esempio n. 8
0
        public static async Task Main()
        {
            await CreateDatabases();

            // Insert, update, and delete some entities

            using (var context = new BlogsContext())
            {
                context.Add(
                    new Blog {
                    Name = "EF Blog", Posts = { new Post {
                                                    Title = "EF Core 3.1!"
                                                }, new Post{
                                                    Title = "EF Core 5.0!"
                                                } }
                });

                await context.SaveChangesAsync();
            }

            using (var context = new BlogsContext())
            {
                var blog = context.Blogs.Include(e => e.Posts).Single();

                blog.Name = "EF Core Blog";
                context.Remove(blog.Posts.First());
                blog.Posts.Add(new Post {
                    Title = "EF Core 6.0!"
                });

                context.SaveChanges();
            }

            // Do an insert that will fail

            using (var context = new BlogsContext())
            {
                try
                {
                    context.Add(new Post {
                        Id = 3, Title = "EF Core 3.1!"
                    });

                    await context.SaveChangesAsync();
                }
                catch (DbUpdateException)
                {
                }
            }

            using (var context =
                       new AuditContext(@"Server=(localdb)\mssqllocaldb;Database=Demo5.Audit;Trusted_Connection=True;ConnectRetryCount=0"))
            {
                foreach (var audit in context.SaveChangesAudits.Include(e => e.Entities).ToList())
                {
                    Console.WriteLine(
                        $"Audit {audit.AuditId} from {audit.StartTime} to {audit.EndTime} was{(audit.Succeeded ? "" : " not")} successful.");

                    foreach (var entity in audit.Entities)
                    {
                        Console.WriteLine($"  {entity.AuditMessage}");
                    }

                    if (!audit.Succeeded)
                    {
                        Console.WriteLine($"  Error: {audit.ErrorMessage}");
                    }
                }
            }
        }