private void CompareCashflows(Core.Entities.Cashflow entity1, Core.Entities.Cashflow entity2, string prefix) { Assert.IsNotNull(entity1, "{0} - entity1", prefix); Assert.IsNotNull(entity2, "{0} - entity2", prefix); Assert.AreEqual(entity1.CashflowId, entity2.CashflowId, "CashflowId"); Assert.AreEqual(entity1.Name, entity2.Name, "Cashflow Name"); Assert.AreEqual(entity1.OpeningBalance, entity2.OpeningBalance, "OpeningBalance"); Assert.AreEqual(entity1.StartDate, entity2.StartDate, "StartDate"); Assert.AreEqual(entity1.RecordCreatedDateTime, entity2.RecordCreatedDateTime, "RecordCreatedDateTime"); Assert.AreEqual(entity1.RecordUpdatedDateTime, entity2.RecordUpdatedDateTime, "RecordUpdatedDateTime"); Assert.IsNotNull(entity1.CashflowBankAccounts, "entity1 CashflowBankAccounts null"); Assert.IsNotNull(entity2.CashflowBankAccounts, "entity2 CashflowBankAccounts null"); Assert.AreEqual(entity1.CashflowBankAccounts.Count, entity2.CashflowBankAccounts.Count, "CashflowBankAccounts qty"); foreach (var e1cba in entity1.CashflowBankAccounts) { Assert.IsTrue(e1cba.BankAccount.BankAccountId > 0, "e1cba.BankAccount.BankAccountId > 0"); var e2cba = entity2.CashflowBankAccounts.FirstOrDefault(o => o.BankAccount.BankAccountId == e1cba.BankAccount.BankAccountId); Assert.IsNotNull(e2cba, "entity1 BankAccount not found in entity2"); CompareCashflowBankAccounts(e1cba, e2cba); } }
public void Initialize() { //repository = container.Resolve<IRepositoryRead<Core.Entities.Cashflow>>(); //repository = container.Resolve<IRepositoryWrite<Core.Entities.Cashflow>>(); repository = container.Resolve <ICashflowRepository>(); testEntity = new Core.Entities.Cashflow(null) { Name = "cflow-" + Guid.NewGuid().ToString(), OpeningBalance = 123M, StartDate = DateTime.Now.Date, CashflowBankAccounts = new List <Core.Entities.CashflowBankAccount>() { new Core.Entities.CashflowBankAccount() { BankAccount = new Core.Entities.BankAccount() { BankAccountId = 1 } }, new Core.Entities.CashflowBankAccount() { BankAccount = new Core.Entities.BankAccount() { BankAccountId = 2 } } } }; }
public void Initialize() { //repository = container.Resolve<IRepositoryRead<Core.Entities.Cashflow>>(); //repository = container.Resolve<IRepositoryWrite<Core.Entities.Cashflow>>(); repository = container.Resolve<ICashflowRepository>(); testEntity = new Core.Entities.Cashflow(null) { Name = "cflow-" + Guid.NewGuid().ToString(), OpeningBalance = 123M, StartDate = DateTime.Now.Date, CashflowBankAccounts = new List<Core.Entities.CashflowBankAccount>() { new Core.Entities.CashflowBankAccount() { BankAccount=new Core.Entities.BankAccount() { BankAccountId=1 } }, new Core.Entities.CashflowBankAccount() { BankAccount=new Core.Entities.BankAccount() { BankAccountId=2 } } } }; }
public int Add(Core.Entities.Cashflow entity) { int id = 0; //try //{ var ef = mapper.Map <Cashflow>(entity); using (FinanceEntities context = factory.CreateContext()) { context.Entry(ef).State = EntityState.Added; context.SaveChanges(); } //read back columns which may have changed entity.CashflowId = ef.CashflowId; entity.RecordCreatedDateTime = ef.RecordCreatedDateTime; entity.RecordUpdatedDateTime = ef.RecordUpdatedDateTime; int i = 0; foreach (var cba in ef.CashflowBankAccounts) { if (entity.CashflowBankAccounts.Count <= i) { break; } var e = entity.CashflowBankAccounts[i]; e.CashflowBankAccountId = cba.CashflowBankAccountId; e.RecordCreatedDateTime = cba.RecordCreatedDateTime; i++; } id = ef.CashflowId; //} //catch (DbEntityValidationException e) //{ // CommonRepository.HandleDbEntityValidationException(e); //} return(id); }
public bool Delete(Core.Entities.Cashflow entity) { //try //{ var ef = mapper.Map <Cashflow>(entity); ef.CashflowBankAccounts.Clear(); using (FinanceEntities context = factory.CreateContext()) { context.Entry(ef).State = EntityState.Deleted; // deletes are cascaded to CashflowBankAccount context.SaveChanges(); } //} //catch (DbEntityValidationException e) //{ // CommonRepository.HandleDbEntityValidationException(e); //} return(true); }
public Core.Entities.Cashflow Read(int id) { Core.Entities.Cashflow entity = null; //try //{ using (FinanceEntities context = factory.CreateContext()) { var ef = (from b in context.Cashflows .Include(a => a.CashflowBankAccounts) .Include("CashflowBankAccounts.BankAccount") .Include("CashflowBankAccounts.BankAccount.Bank") where b.CashflowId == id select b).FirstOrDefault(); entity = mapper.Map <Core.Entities.Cashflow>(ef); } //} //catch (DbEntityValidationException e) //{ // CommonRepository.HandleDbEntityValidationException(e); //} return(entity); }
public bool Update(Core.Entities.Cashflow entity) { //throw new Exception("testing"); //try //{ var ef = mapper.Map <Cashflow>(entity); using (FinanceEntities context = factory.CreateContext()) { // read in children var cbas = from b in context.CashflowBankAccounts where b.CashflowId == ef.CashflowId select b; foreach (var cba in cbas) { if (ef.CashflowBankAccounts.Count(a => a.CashflowBankAccountId == cba.CashflowBankAccountId) == 0) { context.Entry(cba).State = EntityState.Deleted; } else { context.Entry(cba).State = EntityState.Detached; } } foreach (var cba in ef.CashflowBankAccounts) { if (cba.CashflowBankAccountId > 0) { context.Entry(cba).State = EntityState.Modified; } else { context.Entry(cba).State = EntityState.Added; } } context.Entry(ef).State = EntityState.Modified; var s = ShowEntityStates(context); context.SaveChanges(); } //read back data which may have changed entity.RecordUpdatedDateTime = ef.RecordUpdatedDateTime; int i = 0; foreach (var cba in ef.CashflowBankAccounts) { if (entity.CashflowBankAccounts.Count <= i) { break; } var e = entity.CashflowBankAccounts[i]; if (e.CashflowBankAccountId == 0) { e.CashflowBankAccountId = cba.CashflowBankAccountId; e.RecordCreatedDateTime = cba.RecordCreatedDateTime; } i++; } //} //catch (DbEntityValidationException e) //{ // CommonRepository.HandleDbEntityValidationException(e); //} return(true); }