private static void RemoveMultiClass(Func <DbContextMultiClass> createDbContext)
 {
     using (DbContextMultiClass context = createDbContext()) {
         context.Add(new DbContextObject1()
         {
             ItemName = "1"
         });
         context.Add(new DbContextObject2()
         {
             User = "******"
         });
         context.Add(new DbContextObject3()
         {
             Notes = "3"
         });
         context.SaveChanges();
     }
     using (DbContextMultiClass context = createDbContext()) {
         var itemDbContextObject1 = context.dbContextDbSet1.Single();
         var itemDbContextObject2 = context.dbContextDbSet2.Single();
         var itemDbContextObject3 = context.dbContextDbSet3.Single();
         DbContextObject1.Count = 0;
         context.Remove(itemDbContextObject1);
         context.Remove(itemDbContextObject2);
         context.Remove(itemDbContextObject3);
         context.SaveChanges();
         Assert.AreEqual(0, DbContextObject1.Count);
     }
     using (DbContextMultiClass context = createDbContext()) {
         Assert.AreEqual(0, context.dbContextDbSet1.Count());
         Assert.AreEqual(0, context.dbContextDbSet2.Count());
         Assert.AreEqual(0, context.dbContextDbSet3.Count());
     }
 }
 private void Remove(Func <DbContextMultiClass> createDbContext)
 {
     using (var context = createDbContext()) {
         context.Add(new DbContextObject1());
         context.SaveChanges();
     }
     using (DbContextMultiClass context = createDbContext()) {
         context.Remove(context.dbContextDbSet1.Single());
         context.SaveChanges();
     }
     using (DbContextMultiClass context = createDbContext()) {
         Assert.AreEqual(0, context.dbContextDbSet1.Count());
     }
 }
示例#3
0
        public void RollbackDeletedObjectDatabaseValuesDenyPolicy()
        {
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                dbContextMultiClass.Add(new DbContextObject1());
                dbContextMultiClass.SaveChanges();
            }
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = dbContextMultiClass.dbContextDbSet1.FirstOrDefault();

                dbContextMultiClass.PermissionsContainer.SetPermissionPolicy(PermissionPolicy.DenyAllByDefault);

                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > deleteCriteria = (db, obj) => obj.Description == "Good description";
                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > writeCriteria  = (db, obj) => true;

                dbContextMultiClass.PermissionsContainer.AddObjectPermission(SecurityOperation.Delete, OperationState.Allow, deleteCriteria);
                dbContextMultiClass.PermissionsContainer.AddObjectPermission(SecurityOperation.Write, OperationState.Allow, writeCriteria);
                dbContextMultiClass.PermissionsContainer.SetTypePermission(typeof(DbContextObject1), SecurityOperation.Read, OperationState.Allow);
                obj1.Description = "Not good description";
                dbContextMultiClass.SaveChanges();

                dbContextMultiClass.Remove(obj1);

                SecurityTestHelper.FailSaveChanges(dbContextMultiClass);
            }
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = dbContextMultiClass.dbContextDbSet1.FirstOrDefault();
                obj1.Description = "Good description";

                dbContextMultiClass.SaveChanges();
            }
            using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
                DbContextObject1 obj1 = dbContextMultiClass.dbContextDbSet1.FirstOrDefault();

                dbContextMultiClass.PermissionsContainer.SetPermissionPolicy(PermissionPolicy.DenyAllByDefault);

                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > deleteCriteria = (db, obj) => obj.Description == "Good description";
                Expression <Func <DbContextMultiClass, DbContextObject1, bool> > writeCriteria  = (db, obj) => true;
                dbContextMultiClass.PermissionsContainer.AddObjectPermission(SecurityOperation.Delete, OperationState.Allow, deleteCriteria);
                dbContextMultiClass.PermissionsContainer.AddObjectPermission(SecurityOperation.Write, OperationState.Allow, writeCriteria);
                dbContextMultiClass.PermissionsContainer.SetTypePermission(typeof(DbContextObject1), SecurityOperation.Read, OperationState.Allow);

                dbContextMultiClass.Remove(obj1);

                dbContextMultiClass.SaveChanges();
            }
        }
 public void DeleteType()
 {
     using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
         dbContextMultiClass.ResetDatabase();
         dbContextMultiClass.Add(new DbContextObject1());
         dbContextMultiClass.SaveChanges();
     }
     using (DbContextMultiClass dbContextMultiClass = new DbContextMultiClass()) {
         dbContextMultiClass.PermissionsContainer.SetTypePermission <DbContextObject1>(SecurityOperation.Delete, OperationState.Deny);
         DbContextObject1 dbContextObject1 = dbContextMultiClass.dbContextDbSet1.First();
         dbContextMultiClass.Remove(dbContextObject1);
         try {
             dbContextMultiClass.SaveChanges();
             Assert.Fail("Fail");
         }
         catch (Exception e) {
             Assert.AreNotEqual("Fail", e.Message);
         }
     }
 }