예제 #1
0
        public void Attach_Saved_SingleObject()
        {
            var thing = SaveNewThing();

            using (var context = new LoanContext())
            {
                context.StateShouldBe(thing, Detached);
                context.Attach(thing);
                thing.Log("Attached a saved", context);
                context.StateShouldBe(thing, Unchanged);
            }
        }
예제 #2
0
 protected Thing SaveNewThing()
 {
     using (var context = new LoanContext())
     {
         var thing = new Thing();
         context.Update(thing);
         context.StateShouldBe(thing, Added);
         context.SaveChanges();
         context.StateShouldBe(thing, Unchanged);
         return(thing);
     }
 }
예제 #3
0
        public void Construct_SetState_SingleObject()
        {
            using (var context = new LoanContext())
            {
                var thing = new Thing();
                thing.Log("Constructed", context);
                context.StateShouldBe(thing, Detached);

                context.Entry(thing).State = Unchanged;
                thing.Log("Attached", context);
                context.StateShouldBe(thing, Unchanged);
            }
        }
예제 #4
0
        public void Save_Update_SingleObject()
        {
            var thing = SaveNewThing();

            using (var context = new LoanContext())
            {
                context.StateShouldBe(thing, Detached);
                context.Update(thing);
                context.StateShouldBe(thing, Modified);

                context.SaveChanges(); // will update db even no real changes
                context.StateShouldBe(thing, Unchanged);
            }
        }
예제 #5
0
        public void Save_Linq_Include_Update_Graph()
        {
            LogMsg(MethodBase.GetCurrentMethod().Name);
            var xferLoan = SaveNewLoanExAndDetach();

            using (var context = new LoanContext())
            {
                // for the analogous Loan test, this throws since Lender in graph twice
                context.Update(xferLoan);
                context.StateShouldBe(xferLoan, Modified);

                context.SaveChanges(); // updates only Loans since others null
                context.StateShouldBe(xferLoan, Unchanged);
            }
        }
예제 #6
0
        public void ConstructGraph_Attach_SetState()
        {
            LogMsg(MethodBase.GetCurrentMethod().Name);
            using (var context = new LoanContext())
            {
                var loan = new Loan(childrenToo: true);
                loan.Log("Constructed", context);
                context.LoanGraphStateShouldBe(loan, Detached);

                context.Attach(loan).State = Unchanged; // Attach set graph to Added
                loan.Log("Attach and State set", context);
                context.StateShouldBe(loan, Unchanged);
                context.StateShouldBe(loan.Lender, Added);
                context.StateShouldBe(loan.LenderContact, Added);
            }
        }
        public void Construct_Update_Save()
        {
            using (var context = new LoanContext())
            {
                var thing = new Thing();
                thing.Log("Constructed", context);
                context.StateShouldBe(thing, Detached);

                context.Update(thing);
                thing.Log("Updated", context);
                context.StateShouldBe(thing, Added);

                context.SaveChanges();
                thing.Log("SaveChanges", context);
                context.StateShouldBe(thing, Unchanged);
            }
        }
예제 #8
0
        public void Construct_Attach_Save_SingleObject()
        {
            using (var context = new LoanContext())
            {
                var thing = new Thing();
                thing.Log("Constructed", context);
                context.StateShouldBe(thing, Detached);

                context.Attach(thing);
                thing.Log("Attached", context);
                context.StateShouldBe(thing, Added);

                context.SaveChanges();
                thing.Log("SaveChanges", context);
                context.StateShouldBe(thing, Unchanged);
            }
        }
예제 #9
0
        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);
            }
        }
예제 #10
0
        public void Save_Find_Update_SingleObject()
        {
            var thing = SaveNewThing();

            using (var context = new LoanContext())
            {
                var foundThing = context.Find <Thing>(thing.Id);
                foundThing.ShouldNotBeNull();
                foundThing.Log("Find", context);
                context.StateShouldBe(foundThing, Unchanged);

                foundThing.Name = "Peoria";
                context.StateShouldBe(foundThing, Modified);

                context.SaveChanges();
                context.StateShouldBe(foundThing, Unchanged);
            }
        }
예제 #11
0
        public void Save_Find_Delete_SingleObject()
        {
            var thing = SaveNewThing();

            using (var context = new LoanContext())
            {
                var foundThing = context.Find <Thing>(thing.Id);
                foundThing.ShouldNotBeNull();
                foundThing.Log("Find", context);
                context.StateShouldBe(foundThing, Unchanged);

                context.Remove(foundThing);
                context.StateShouldBe(foundThing, Deleted);

                context.SaveChanges();
                context.StateShouldBe(foundThing, Detached);
            }
        }
예제 #12
0
        public void Save_Find_Update_Graph()
        {
            LogMsg(MethodBase.GetCurrentMethod().Name);
            var loan = SaveNewLoan();

            using (var context = new LoanContext())
            {
                var foundLoan = context.Find <Loan>(loan.Id);
                foundLoan.ShouldNotBeNull();
                foundLoan.Log("Find", context);
                context.StateShouldBe(foundLoan, Unchanged);
                foundLoan.Name = "Peoria";
                context.StateShouldBe(foundLoan, Modified);
                context.SaveChanges();
                context.StateShouldBe(foundLoan, Unchanged);
                foundLoan = context.Find <Loan>(loan.Id);
                foundLoan.Name.ShouldBe("Peoria");
            }
        }
예제 #13
0
        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");
            }
        }
예제 #14
0
        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);
        }
        public void Save_Linq_Include_Update_Graph()
        {
            LogMsg(MethodBase.GetCurrentMethod().Name);
            var xferLoan = SaveNewLoanExAndDetach();

            using (var context = new LoanContext())
            {
                // in case of Loan with childrenToo, this throws since Lender in graph twice
                context.Update(xferLoan);
                context.StateShouldBe(xferLoan, Modified);
            }
        }
        public void Save_Find()
        {
            var thing = SaveNewThing();

            using (var context = new LoanContext())
            {
                var foundThing = context.Find <Thing>(thing.Id);
                foundThing.ShouldNotBeNull();
                foundThing.Log("Find", context);
                context.StateShouldBe(foundThing, Unchanged);
            }
        }
        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);
            }
        }
예제 #18
0
        public void Save_Linq_Include_Attach_Graph()
        {
            LogMsg(MethodBase.GetCurrentMethod().Name);
            var xferLoan = SaveNewLoanExAndDetach();

            using (var context = new LoanContext())
            {
                // for the analogous Loan test, this throws since Lender in graph twice
                context.Attach(xferLoan);
                context.StateShouldBe(xferLoan, Unchanged);
            }
        }
        public void Save_Linq_Include_TrackGraph_Func()
        {
            LogMsg(MethodBase.GetCurrentMethod().Name);
            var xferLoan = SaveNewLoanAndDetach();

            using (var context = new LoanContext())
            {
                context.ChangeTracker.TrackGraph(xferLoan, Unchanged, (node, state) => {
                    node.Entry.State = Unchanged;
                    if (node.Entry.Entity is Loan)
                    {
                        node.Entry.State = Modified;
                    }
                    return(!(node.Entry.Entity is LenderContact));
                });
                xferLoan.Log("TrackGraph", context);
                context.StateShouldBe(xferLoan, Modified);
                context.StateShouldBe(xferLoan.Lender, Unchanged);
                context.StateShouldBe(xferLoan.LenderContact, Unchanged);
            }
        }
예제 #20
0
        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);
        }
예제 #21
0
        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();
            }
        }
예제 #22
0
        public void Throw_Add_Duplicate_Thing()
        {
            LogMsg(MethodBase.GetCurrentMethod().Name);
            var thing = SaveNewThingAndDetach();

            using (var context = new LoanContext())
            {
                context.Add(thing);
                context.StateShouldBe(thing, Added);

                try
                {
                    context.SaveChanges();
                    true.ShouldBeFalse();
                }
                catch (DbUpdateException)
                {
                    true.ShouldBeTrue(); // duplicate key since already there.
                }
            }
        }
예제 #23
0
 protected LoanEx SaveNewLoanEx()
 {
     using (var context = new LoanContext())
     {
         var loan = new LoanEx(childrenToo: true);
         context.Update(loan);
         context.StateShouldBe(loan, Added);
         context.StateShouldBe(loan.Lender, Added);
         context.StateShouldBe(loan.LenderContact, Added);
         context.SaveChanges();
         context.StateShouldBe(loan, Unchanged);
         context.StateShouldBe(loan.Lender, Unchanged);
         context.StateShouldBe(loan.LenderContact, Unchanged);
         return(loan);
     }
 }
        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();
            }
        }
예제 #25
0
        public void Save_Linq_Include_SetState_Graph()
        {
            LogMsg(MethodBase.GetCurrentMethod().Name);

            var xferLoan = SaveNewLoanAndDetach();

            using (var context = new LoanContext())
            {
                context.Entry(xferLoan).State = Modified;
                xferLoan.Log("After set State on root", context);
                context.StateShouldBe(xferLoan, Modified);
                context.StateShouldBe(xferLoan.Lender, Detached);
                context.StateShouldBe(xferLoan.LenderContact, Detached);

                context.SaveChanges();
                context.StateShouldBe(xferLoan, Unchanged);
                context.StateShouldBe(xferLoan.Lender, Detached);
                context.StateShouldBe(xferLoan.LenderContact, Detached);
            }
        }