public void AddItemInCollection_DenyCreateObject() { CreateObjects(); using (DbContextICollectionProperty context = new DbContextICollectionProperty()) { context.PermissionsContainer.AddObjectPermission <DbContextICollectionProperty, OneToManyICollection_Many>( SecurityOperation.Create, OperationState.Deny, (c, t) => t.Name == "4"); OneToManyICollection_One.Count = 0; OneToManyICollection_Many.Count = 0; OneToManyICollection_One one = context.OneToManyICollection_One.Include(p => p.Collection).First(); OneToManyICollection_Many oneToManyICollection_Many = new OneToManyICollection_Many(); oneToManyICollection_Many.Name = "4"; one.Collection.Add(oneToManyICollection_Many); AssertFail(context); one.Collection.Add(oneToManyICollection_Many); oneToManyICollection_Many.Name = "5"; context.SaveChanges(); Assert.AreEqual(2, OneToManyICollection_One.Count); Assert.AreEqual(9, OneToManyICollection_Many.Count); } using (DbContextICollectionProperty context = new DbContextICollectionProperty()) { OneToManyICollection_One.Count = 0; OneToManyICollection_Many.Count = 0; OneToManyICollection_One one = context.OneToManyICollection_One.Include(p => p.Collection).First(); Assert.AreEqual(4, one.Collection.Count); Assert.IsTrue(one.Collection.Any(p => p.Name == "5")); Assert.AreEqual(2, OneToManyICollection_One.Count); Assert.AreEqual(8, OneToManyICollection_Many.Count); } }
public void CreateSecurityCollectionObject_DenyNavigationMember_Read() { CreateObjects(); using (DbContextICollectionProperty context = new DbContextICollectionProperty()) { context.PermissionsContainer.AddMemberPermission <DbContextICollectionProperty, OneToManyICollection_Many>(SecurityOperation.Read, OperationState.Deny, "One", (p, d) => true); OneToManyICollection_One.Count = 0; OneToManyICollection_Many.Count = 0; OneToManyICollection_Many one = context.OneToManyICollection_Many.Include(p => p.One).First(); Assert.IsNull(one.One); Assert.AreEqual(OneToManyICollection_One.Count, 2); Assert.AreEqual(OneToManyICollection_Many.Count, 2); } }
public static void CreateObjectsWithOutCollection() { using (DbContextICollectionProperty context = new DbContextICollectionProperty()) { OneToManyICollection_One one = new OneToManyICollection_One(); one.Name = "1"; context.Add(one); for (int i = 1; i < 4; i++) { string indexString = i.ToString(); OneToManyICollection_Many company = new OneToManyICollection_Many(); company.Name = i.ToString(); context.Add(company); } context.SaveChanges(); } }
public void ChengeItemInCollection_DenyWriteCollectionAddExistingObjectInCollection() { CreateObjectsWithOutCollection(); using (DbContextICollectionProperty context = new DbContextICollectionProperty()) { context.PermissionsContainer.AddMemberPermission <DbContextICollectionProperty, OneToManyICollection_One>( SecurityOperation.Write, OperationState.Deny, "Collection", (c, t) => true); OneToManyICollection_One one = context.OneToManyICollection_One.Include(p => p.Collection).First(); OneToManyICollection_Many many = context.OneToManyICollection_Many.First(p => p.Name == "1"); one.Collection.Add(many); AssertFail(context); } using (DbContextICollectionProperty context = new DbContextICollectionProperty()) { OneToManyICollection_One one = context.OneToManyICollection_One.Include(p => p.Collection).First(); Assert.AreEqual(0, one.Collection.Count); Assert.AreEqual(3, context.OneToManyICollection_Many.Count()); } }
public void ChangeItemInCollection_DenyAddingNewObjectInCollection() { CreateObjects(); using (DbContextICollectionProperty context = new DbContextICollectionProperty()) { { context.PermissionsContainer.AddMemberPermission <DbContextICollectionProperty, OneToManyICollection_One>( SecurityOperation.Write, OperationState.Deny, "Collection", (c, t) => true); OneToManyICollection_One one = context.OneToManyICollection_One.Include(p => p.Collection).First(); OneToManyICollection_Many many = new OneToManyICollection_Many(); many.Name = "4"; one.Collection.Add(many); var stateManager = context.ChangeTracker.GetStateManager(); var entityType = context.Model.GetEntityTypes().First(p => p.ClrType == typeof(OneToManyICollection_Many)); var property = entityType.GetForeignKeys().First().Properties.First(); var entity = stateManager.Entries.Last()[property]; AssertFail(context); } } }
public void AddItemInCollection() { CreateObjects(); using (DbContextICollectionProperty context = new DbContextICollectionProperty()) { OneToManyICollection_One.Count = 0; OneToManyICollection_Many.Count = 0; OneToManyICollection_One one = context.OneToManyICollection_One.Include(p => p.Collection).First(); OneToManyICollection_Many oneToManyICollection_Many = new OneToManyICollection_Many(); oneToManyICollection_Many.Name = "4"; one.Collection.Add(oneToManyICollection_Many); context.SaveChanges(); Assert.AreEqual(2, OneToManyICollection_One.Count); Assert.AreEqual(8, OneToManyICollection_Many.Count); } using (DbContextICollectionProperty context = new DbContextICollectionProperty()) { OneToManyICollection_One.Count = 0; OneToManyICollection_Many.Count = 0; OneToManyICollection_One one = context.OneToManyICollection_One.Include(p => p.Collection).First(); Assert.AreEqual(4, one.Collection.Count); Assert.AreEqual(2, OneToManyICollection_One.Count); Assert.AreEqual(8, OneToManyICollection_Many.Count); } }