public void Test_EF_TransactionId()
        {
            var evs = new List<AuditEvent>();
            Audit.Core.Configuration.Setup()
                .Use(_ => _.OnInsert(ev =>
                {
                    evs.Add(ev);
                }));

            var id = Guid.NewGuid().ToString().Substring(0, 8);
            using (var context = new BlogsContext())
            {
                using (var tran = context.Database.BeginTransaction())
                {
                    var blog = context.Blogs.First();
                    blog.Title = id;
                    context.SaveChanges();
                    tran.Commit();
                }
            }

            Assert.AreEqual(1, evs.Count);
            Assert.IsTrue(!string.IsNullOrWhiteSpace(evs[0].GetEntityFrameworkEvent().TransactionId));
        }
Ejemplo n.º 2
0
        public void Test_EF_MapMultipleTypesToSameAuditType()
        {
            var guid = Guid.NewGuid().ToString();

            Audit.Core.Configuration.Setup()
            .UseEntityFramework(_ => _
                                .UseDbContext <BlogsContext>()
                                .AuditTypeExplicitMapper(m => m
                                                         .Map <Blog, CommonAudit>((blog, commonAudit) =>
            {
                commonAudit.EntityType = "Blog";
                commonAudit.EntityId   = blog.Id;
                commonAudit.Title      = blog.Title;
                commonAudit.Group      = guid;
            })
                                                         .Map <Post, CommonAudit>((post, commonAudit) =>
            {
                commonAudit.EntityType = "Post";
                commonAudit.EntityId   = post.Id;
                commonAudit.Title      = post.Title;
                commonAudit.Group      = guid;
            })
                                                         .AuditEntityAction <IAuditEntity>((ev, entry, entity) =>
            {
                entity.AuditAction = entry.Action;
                entity.AuditDate   = DateTime.Now;
                entity.AuditUser   = Environment.UserName;
                entity.Exception   = ev.GetEntityFrameworkEvent().ErrorMessage;
            })));

            using (var context = new BlogsContext())
            {
                context.Blogs.Add(new Blog {
                    BloggerName = guid, Title = "TestBlog"
                });
                context.SaveChanges();

                var blog = context.Blogs.First(b => b.BloggerName == guid);

                context.Posts.Add(new Post()
                {
                    BlogId = blog.Id, Title = "TestPost", Content = guid
                });
                context.SaveChanges();

                var post = context.Posts.First(b => b.Content == guid);

                var audits = context.CommonAudits.Where(a => a.Group == guid).OrderBy(a => a.AuditDate).ToList();

                Assert.AreEqual(2, audits.Count);
                Assert.AreEqual("Blog", audits[0].EntityType);
                Assert.AreEqual(blog.Id, audits[0].EntityId);
                Assert.AreEqual(blog.Title, audits[0].Title);

                Assert.AreEqual("Post", audits[1].EntityType);
                Assert.AreEqual(post.Id, audits[1].EntityId);
                Assert.AreEqual(post.Title, audits[1].Title);

                Assert.AreEqual(Environment.UserName, audits[0].AuditUser);
                Assert.AreEqual(Environment.UserName, audits[1].AuditUser);
            }
        }
Ejemplo n.º 3
0
        public void Test_EF_MapOneTypeToMultipleAuditTypes()
        {
            var guid = Guid.NewGuid().ToString();

            Audit.Core.Configuration.Setup()
            .UseEntityFramework(_ => _
                                .UseDbContext <BlogsContext>()
                                .AuditTypeExplicitMapper(m => m
                                                         .Map <Blog>(mapper: entry => entry.Action == "Update" ? typeof(BlogAudit) : typeof(CommonAudit),
                                                                     entityAction: (ev, entry, entity) =>
            {
                if (entry.Action == "Update")
                {
                    Assert.AreEqual(typeof(BlogAudit), entity.GetType());
                    var ba    = entity as BlogAudit;
                    ba.BlogId = (int)entry.PrimaryKey.First().Value;
                }
                else
                {
                    Assert.AreEqual(typeof(CommonAudit), entity.GetType());
                    var ca        = entity as CommonAudit;
                    ca.Group      = guid;
                    ca.EntityType = entry.EntityType.Name;
                    ca.EntityId   = (int)entry.PrimaryKey.First().Value;
                }
            })
                                                         .Map <Post, PostAudit>((post, postAudit) =>
            {
                postAudit.PostId = post.Id;
            })
                                                         .AuditEntityAction <IAuditEntity>((ev, entry, entity) =>
            {
                entity.AuditAction = entry.Action;
                entity.AuditDate   = DateTime.Now;
                entity.AuditUser   = Environment.UserName;
                entity.Exception   = ev.GetEntityFrameworkEvent().ErrorMessage;
            })));

            using (var context = new BlogsContext())
            {
                context.Blogs.Add(new Blog {
                    BloggerName = guid, Title = "TestBlog"
                });
                context.SaveChanges(); // CommonAudit

                var blog     = context.Blogs.First(b => b.BloggerName == guid);
                var newTitle = guid.Substring(0, 10);
                blog.Title = newTitle;
                context.SaveChanges(); // BlogAudit

                var audits     = context.CommonAudits.Where(a => a.Group == guid).OrderBy(a => a.AuditDate).ToList();
                var blogaudits = context.BlogsAudits.Where(x => x.Title == newTitle).ToList();

                Assert.IsNotNull(blogaudits);
                Assert.AreEqual(1, blogaudits.Count);
                Assert.AreEqual(blog.Id, blogaudits[0].BlogId);
                Assert.AreEqual(1, audits.Count);
                Assert.AreEqual("Blog", audits[0].EntityType);
                Assert.AreEqual(blog.Id, audits[0].EntityId);
                Assert.AreEqual("TestBlog", audits[0].Title);
                Assert.AreEqual(Environment.UserName, audits[0].AuditUser);
            }
        }