public virtual void AttachAddedNonExistingTest() { long id; IRepository target; TestEntity entity; using (target = GetInitializedRepository()) { id = target.GetStoreId <TestEntity, long>(); entity = target.CreateEntity <TestEntity>(); entity.Id = id; entity.Name = "test" + id; target.Add(entity); target.CommitChanges(); // the store may have generated it id = entity.Id; } entity.StringProperty = "testValue"; var entity1 = new TestEntity { Name = "test100", Created = Facility.Clock.UtcNow, Updated = Facility.Clock.UtcNow, }; entity1.SetUpdated(); using (target = GetInitializedRepository()) { var id1 = target.GetStoreId <TestEntity, long>(); entity1.Id = id1; target.AttachEntity(entity1, EntityState.Added); target.CommitChanges(); id1 = entity1.Id; var entity2 = target.GetByStoreId <TestEntity, long>(id1); Assert.AreEqual(entity1.Id, entity2.Id); Assert.AreEqual(entity1, entity2); Assert.IsTrue(ReferenceEquals(entity1, entity2)); } }
public virtual void AttachDeletedNonExistingTest() { long id; IRepository target; TestEntity entity; using (target = GetInitializedRepository()) { id = target.GetStoreId <TestEntity, long>(); entity = target.CreateEntity <TestEntity>(); entity.Id = id; entity.Name = "test" + id; target.Add(entity); target.CommitChanges(); // the store may have generated it id = entity.Id; } entity.StringProperty = "testValue"; var entity1 = new TestEntity { Name = "test101", Created = Facility.Clock.UtcNow, Updated = Facility.Clock.UtcNow, }; entity1.SetUpdated(); Type targetType = null; try { using (target = GetInitializedRepository()) { targetType = target.GetType(); var id1 = target.GetStoreId <TestEntity, long>(); entity1.Id = id1; target.AttachEntity(entity1, EntityState.Deleted); target.CommitChanges(); Assert.IsNull(target.GetByStoreId <TestEntity, long>(id1)); } } catch (DbUpdateConcurrencyException) { Assert.IsTrue(typeof(EFRepositoryBase).IsAssignableFrom(targetType)); } // Add here more similar exception handlers for expected exceptions from different types of repositories. catch (Exception x) { TestContext.WriteLine("{0}", x.DumpString()); throw; } }