コード例 #1
0
        public void Test_EF_IgnoreOverrideInheritance()
        {
            var guid = Guid.NewGuid().ToString().Substring(0, 6);
            var evs  = new List <AuditEvent>();

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsContext>(x => x
                                       .IncludeEntityObjects(true));
            Audit.Core.Configuration.Setup()
            .AuditDisabled(false)
            .UseDynamicProvider(x => x
                                .OnInsertAndReplace(ev =>
            {
                evs.Add(ev);
            }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd);

            var options = new DbContextOptionsBuilder <BlogsMemoryContext>()
                          .UseInMemoryDatabase(databaseName: "database_test")
                          .Options;
            var id = Rnd.Next();

            using (var ctx = new BlogsMemoryContext(options))
            {
                var user = new User()
                {
                    Id       = id,
                    Name     = "fede",
                    Password = "******",
                    Token    = "aaabbb"
                };
                ctx.Users.Add(user);
                Audit.Core.Configuration.AuditDisabled = true;
                ctx.SaveChanges();
                Audit.Core.Configuration.AuditDisabled = false;

                var usr = ctx.Users.First(u => u.Id == id);
                usr.Password = "******";
                usr.Token    = "xxxaaa";
                ctx.SaveChanges();

                ctx.Users.Remove(user);
                ctx.SaveChanges();
            }

            Assert.AreEqual(2, evs.Count);
            Assert.AreEqual(1, evs[0].GetEntityFrameworkEvent().Entries.Count);
            var entry = evs[0].GetEntityFrameworkEvent().Entries[0];

            Assert.AreEqual(1, entry.Changes.Count);
            var changeToken = entry.Changes.First(_ => _.ColumnName == "Token");

            Assert.AreEqual("***", changeToken.OriginalValue);
            Assert.AreEqual("***", changeToken.NewValue);
            Assert.IsFalse(entry.ColumnValues.ContainsKey("Password"));
            Assert.AreEqual("***", entry.ColumnValues["Token"]);
        }
コード例 #2
0
        public void Test_EF_Provider_ExplicitMapper_MapExplicit()
        {
            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsContext>(x => x
                                       .IncludeEntityObjects(false)
                                       .ExcludeTransactionId(true));
            Audit.Core.Configuration.Setup()
            .AuditDisabled(false)
            .UseEntityFramework(ef => ef
                                .AuditTypeExplicitMapper(config => config
                                                         .MapExplicit <UserAudit>(ee => ee.Table == "User", (ee, userAudit) =>
            {
                userAudit.AuditId   = 1;
                userAudit.UserId    = (int)ee.ColumnValues["Id"];
                userAudit.AuditUser = "******";
            })
                                                         .Map((User user, UserAudit userAudit) =>
            {
                // Should never get here, since the user table is handled explicitly
                userAudit.Action = "Invalid";
            })
                                                         .AuditEntityAction((ee, ent, obj) =>
            {
                ((UserAudit)obj).Action     = ent.Action;
                ((UserAudit)obj).AuditUser += "er";
            }))
                                .IgnoreMatchedProperties(false));

            var options = new DbContextOptionsBuilder <BlogsMemoryContext>()
                          .UseInMemoryDatabase(databaseName: "database_user_test")
                          .Options;
            var id = Rnd.Next();

            using (var ctx = new BlogsMemoryContext(options))
            {
                ctx.Database.EnsureDeleted();
                ctx.Database.EnsureCreated();

                var user = new User()
                {
                    Id       = id,
                    Name     = "test",
                    Password = "******",
                    Token    = "token"
                };
                ctx.Users.Add(user);
                ctx.SaveChanges();
            }

            // Assert
            using (var ctx = new BlogsMemoryContext(options))
            {
                var audit = ctx.UserAudits.Single(u => u.UserId == id);
                Assert.AreEqual(1, audit.AuditId);
                Assert.AreEqual("Insert", audit.Action);
                Assert.AreEqual("user", audit.AuditUser);
                Assert.AreEqual("test", audit.Name);
                Assert.AreEqual(id, audit.UserId);
            }
        }
コード例 #3
0
        public void Test_EF_SaveChangesOverride()
        {
            var evs = new List <AuditEvent>();

            Audit.EntityFramework.Configuration.Setup()
            .ForContext <BlogsContext>(x => x
                                       .IncludeEntityObjects(true));
            Audit.Core.Configuration.Setup()
            .AuditDisabled(false)
            .UseDynamicProvider(x => x
                                .OnInsertAndReplace(ev =>
            {
                evs.Add(ev);
            }))
            .WithCreationPolicy(EventCreationPolicy.InsertOnEnd);

            var options = new DbContextOptionsBuilder <BlogsMemoryContext>()
                          .UseInMemoryDatabase(databaseName: "database_test")
                          .Options;
            var id = Rnd.Next();

            using (var ctx = new BlogsMemoryContext(options))
            {
                var user = new User()
                {
                    Id       = id,
                    Name     = "fede",
                    Password = "******",
                    Token    = "aaabbb"
                };
                ctx.Users.Add(user);
                ctx.SaveChanges();

                ctx.Users.Remove(user);
                ctx.SaveChanges();
            }

            Assert.AreEqual(2, evs.Count);
        }