public void AssignOldEntity() { if (Id == 0) { HirePro.Framework.Core.Exception.ExceptionFactory.BusinessLogicException.AddMessage("Id Cannot Be Zero").Raise(); } OldEntity = EntityRepository <U> .Get(Id); if (OldEntity != null) { EntityRepository <U> .Detach(OldEntity); } }
public void Test_EntityRepo_AttachDetach() { var dbService = new TestDatabaseService(); var repository = new EntityRepository(dms, dbService, new SequenceProvider(dbService)); using (var ctx = dbService.GetDatabaseContext(true)) { #region prepare data var jordan = new Author() { FirstName = "Robert", LastName = "Jordan", IsAlive = false, Born = new DateTime(1948, 10, 17), Rating = 10.0m }; var feist = new Author() { FirstName = "Raymond", LastName = "Feist", IsAlive = true, Born = new DateTime(1963, 2, 14), Rating = 6.7m }; var fb1 = new Book() { Title = "The Apprentice", Price = 19.90m }; var fb2 = new Book() { Title = "The Magician", Price = 17.10m }; repository.Create(jordan); repository.Create(feist); repository.Create(fb1); repository.Create(fb2); #endregion repository.Attach(feist, new Relation("author", fb1)); var rel2 = new Relation("author", fb2); var writtenOn = new DateTime(1996, 4, 25); rel2.SetData<DateTime>("WrittenOn", writtenOn); repository.Attach(feist, rel2); var q = new EntityQuery2("author", feist.Id); q.AddProperties("FirstName", "lastname", "isalive", "born", "rating"); q.Include("book", "author"); var e = repository.Read(q); Assert.AreEqual(2, e.GetManyRelations("book", "author").Count()); var bq = new EntityQuery2("book"); bq.Include("author", "author"); var bes = repository.Search(bq); foreach (var be in bes) { Assert.AreEqual(1, be.RelationsData.Count); Assert.AreEqual(feist.Id, be.GetSingleRelation("author", "author").Entity.Id); if (be.Id == fb2.Id) Assert.AreEqual(writtenOn, be.GetSingleRelation("author", "author").GetData<DateTime>("writtenon")); } repository.Detach(feist, new Relation("author", fb1)); e = repository.Read(q); Assert.AreEqual(1, e.GetManyRelations("book", "author").Count()); repository.Attach(fb1, new Relation("author", feist)); e = repository.Read(q); Assert.AreEqual(2, e.GetManyRelations("book", "author").Count()); repository.Detach(fb1, new Relation("author", feist)); e = repository.Read(q); Assert.AreEqual(1, e.GetManyRelations("book", "author").Count()); bool ex = false; try { repository.Attach(fb2, new Relation("author", jordan)); } catch (Exception) { ex = true; } Assert.IsTrue(ex, "Exception not thrown when attaching two authors to single book"); } }