public void ConstructGraph_Add_Save_Delete() { var loan = SaveNewLoan(); using (var context = new LoanContext()) { var foundLoan = context.Set <Loan>() .Include(o => o.Lender) .Include(o => o.LenderContact) .SingleOrDefault(o => o.Id == loan.Id); foundLoan.ShouldNotBeNull(); foundLoan.Log("Find", context); context.StateShouldBe(foundLoan, Unchanged); context.Remove(foundLoan); foundLoan.Log("Remove", context); context.StateShouldBe(foundLoan, Deleted); context.StateShouldBe(foundLoan.Lender, Unchanged); // not cascading delete context.StateShouldBe(foundLoan.LenderContact, Unchanged); context.SaveChanges(); foundLoan.Log("SaveChanged", context); context.StateShouldBe(foundLoan, Detached); context.StateShouldBe(foundLoan.Lender, Unchanged); // not cascading delete context.StateShouldBe(foundLoan.LenderContact, Unchanged); } }
protected Loan SaveNewLoanAndDetach() { var loan = SaveNewLoan(); Loan xferLoan = null; using (var context = new LoanContext()) { var foundLoan = context.Set <Loan>() .Include(o => o.Lender) .Include(o => o.LenderContact) .SingleOrDefault(o => o.Id == loan.Id); foundLoan.ShouldNotBeNull(); foundLoan.Log("Find", context); context.LoanGraphStateShouldBe(foundLoan, Unchanged); foundLoan.Lender.ShouldNotBeNull(); foundLoan.LenderContact.ShouldNotBeNull(); object.ReferenceEquals(foundLoan.Lender, foundLoan.LenderContact.Lender).ShouldBeTrue(); xferLoan = JsonConvert.DeserializeObject <Loan>(JsonConvert.SerializeObject(foundLoan)); context.LoanGraphStateShouldBe(xferLoan, Detached); xferLoan.Log("After Deserialize", context); } return(xferLoan); }
public void Save_Find_AsNoTracking() { var thing = SaveNewThing(); using (var context = new LoanContext()) { var foundThing = context.Set <Thing>().Where(o => o.Id == thing.Id).AsNoTracking().SingleOrDefault(); foundThing.ShouldNotBeNull(); foundThing.Log("Find", context); context.StateShouldBe(foundThing, Detached); } }
public void Save_Linq_Graph() { LogMsg(MethodBase.GetCurrentMethod().Name); var loan = SaveNewLoan(); using (var context = new LoanContext()) { var foundLoan = context.Set <Loan>() .SingleOrDefault(o => o.Id == loan.Id); foundLoan.ShouldNotBeNull(); foundLoan.Log("Find", context); context.StateShouldBe(foundLoan, Unchanged); foundLoan.Lender.ShouldBeNull(); foundLoan.LenderContact.ShouldBeNull(); } }
public void Construct_WithExisting_Children_SetState() { LogMsg(MethodBase.GetCurrentMethod().Name); var xferLoan = SaveNewLoanAndDetach(); var loan = new Loan { Lender = xferLoan.Lender, LenderContact = xferLoan.LenderContact }; using (var context = new LoanContext()) { loan.Log("Constructed with existing children", context); context.LoanGraphStateShouldBe(loan, Detached); context.Entry(loan).State = Added; loan.Log("State Set", context); context.StateShouldBe(loan, Added); context.StateShouldBe(loan.Lender, Detached); context.StateShouldBe(loan.LenderContact, Detached); context.SaveChanges(); // inserts into only Loans table context.StateShouldBe(loan, Unchanged); context.StateShouldBe(loan.Lender, Detached); context.StateShouldBe(loan.LenderContact, Detached); } using (var context = new LoanContext()) { var foundLoan = context.Set <Loan>() .Include(o => o.Lender) .Include(o => o.LenderContact) .SingleOrDefault(o => o.Id == loan.Id); foundLoan.ShouldNotBeNull(); foundLoan.Lender.ShouldNotBeNull(); foundLoan.LenderContact.ShouldNotBeNull(); foundLoan.Lender.Id.ShouldBe(loan.Lender.Id); foundLoan.LenderContact.Id.ShouldBe(loan.LenderContact.Id); ReferenceEquals(foundLoan.Lender, foundLoan.LenderContact.Lender).ShouldBeTrue(); } }
public void Update_Thing() { LogMsg(MethodBase.GetCurrentMethod().Name); var thing = SaveNewThingAndDetach(); using (var context = new LoanContext()) { context.Update(thing); context.StateShouldBe(thing, Modified); thing.Name = "Peoria"; context.StateShouldBe(thing, Modified); context.SaveChanges(); } using (var context = new LoanContext()) { thing = context.Set <Thing>().Find(thing.Id); thing.ShouldNotBeNull(); thing.Name.ShouldBe("Peoria"); } }
protected Thing SaveNewThingAndDetach() { var thing = SaveNewThing(); Thing xferThing = null; using (var context = new LoanContext()) { var foundThing = context.Set <Thing>() .SingleOrDefault(o => o.Id == thing.Id); foundThing.ShouldNotBeNull(); foundThing.Log("Find", context); context.StateShouldBe(foundThing, Unchanged); xferThing = JsonConvert.DeserializeObject <Thing>(JsonConvert.SerializeObject(foundThing)); context.StateShouldBe(xferThing, Detached); xferThing.Log("After Deserialize", context); } return(xferThing); }
protected LoanEx SaveNewLoanExAndDetach() { var loan = SaveNewLoanEx(); LoanEx xferLoan = null; using (var context = new LoanContext()) { var foundLoan = context.Set <LoanEx>() .SingleOrDefault(o => o.Id == loan.Id); foundLoan.ShouldNotBeNull(); foundLoan.Log("Find", context); context.StateShouldBe(foundLoan, Unchanged); foundLoan.LenderId.ShouldNotBeNull(); foundLoan.LenderContactId.ShouldNotBeNull(); xferLoan = JsonConvert.DeserializeObject <LoanEx>(JsonConvert.SerializeObject(foundLoan)); context.StateShouldBe(xferLoan, Detached); xferLoan.Log("After Deserialize", context); } return(xferLoan); }
public async Task <IEnumerable <TEntity> > GetAllAsync() { using LoanContext context = new LoanContext(); return(await context.Set <TEntity>().ToListAsync()); }