Ejemplo n.º 1
0
        public void LifecycleTest_7_5()
        {
            LinqBooksDataContext context1 = new LinqBooksDataContext();
            LinqBooksDataContext context2 = new LinqBooksDataContext();

            context1.Log = Console.Out;
            context2.Log = Console.Out;

            Guid Id = new Guid("92f10ca6-7970-473d-9a25-1ff6cab8f682");

            Subject editingSubject = context1.Subject.Where(s => s.ID == Id).SingleOrDefault();

            Console.WriteLine("Before Change:");
            ObjectDumper.Write(editingSubject);
            ObjectDumper.Write(context2.Subject.Where(s => s.ID == Id));

            editingSubject.Description = @"Testing update";

            Console.WriteLine("After Change:");
            ObjectDumper.Write(context1.Subject.Where(s => s.ID == Id));
            ObjectDumper.Write(context2.Subject.Where(s => s.ID == Id));

            context1.SubmitChanges();

            Console.WriteLine("After Submit Changes:");
            ObjectDumper.Write(context1.Subject.Where(s => s.ID == Id));
            ObjectDumper.Write(context2.Subject.Where(s => s.ID == Id));
            LinqBooksDataContext context3 = new LinqBooksDataContext();

            ObjectDumper.Write(context3.Subject.Where(s => s.ID == Id));

            //Reset values
            editingSubject.Description = "Original Value";
            context1.SubmitChanges();
        }
Ejemplo n.º 2
0
        public void ConcurrencyDisplayingChanges_8_6()
        {
            LinqBooksDataContext context = new LinqBooksDataContext();

            //Make some changes
            this.MakeConcurrentChanges(context);

            try
            {
                context.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (ChangeConflictException)
            {
                var exceptionDetail =
                    from conflict in context.ChangeConflicts
                    from member in conflict.MemberConflicts
                    select new
                {
                    TableName     = Helpers.GetTableName(context, conflict.Object),
                    MemberName    = member.Member.Name,
                    CurrentValue  = member.CurrentValue.ToString(),
                    DatabaseValue = member.DatabaseValue.ToString(),
                    OriginalValue = member.OriginalValue.ToString()
                };
                ObjectDumper.Write(exceptionDetail);
            }
        }
Ejemplo n.º 3
0
        public void ConcurrencyKeepChanges_8_4()
        {
            LinqBooksDataContext context = this.NewContext;

            //Make some changes
            this.MakeConcurrentChanges(context);

            try
            {
                context.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (ChangeConflictException)
            {
                context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                //resubmit the merged values
                context.SubmitChanges();
            }
        }
Ejemplo n.º 4
0
        public void UpdateSubject(Subject cachedSubject)
        {
            LinqBooksDataContext context = new LinqBooksDataContext();

            context.Log = Console.Out;
            context.Subject.Attach(cachedSubject);
            cachedSubject.Name = @"Testing update";

            context.SubmitChanges();
        }
Ejemplo n.º 5
0
        public void UpdateProcedures_8_16()
        {
            LinqBooksDataContext context = this.NewContext;
            var changingAuthor           = context.Author.FirstOrDefault <Author>();

            changingAuthor.FirstName = "Changing";
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
            {
                context.SubmitChanges();
                //Let the transaction rollback
            }
        }
Ejemplo n.º 6
0
        public void TransactionsSqlTransactionScope_8_8()
        {
            LinqBooksDataContext context = this.NewContext;

            this.MakeConcurrentChanges(context);

            using (System.Transactions.TransactionScope scope =
                       new System.Transactions.TransactionScope())
            {
                context.SubmitChanges(ConflictMode.ContinueOnConflict);
                scope.Complete();
            }
        }
Ejemplo n.º 7
0
        public void ConcurrencyOverwriteCurrentValues_8_5()
        {
            LinqBooksDataContext context = this.NewContext;

            //Make some changes
            this.MakeConcurrentChanges(context);

            try
            {
                context.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (ChangeConflictException)
            {
                context.ChangeConflicts.ResolveAll(RefreshMode.OverwriteCurrentValues);
            }
        }
Ejemplo n.º 8
0
        public void TransactionsDataContext_8_8()
        {
            LinqBooksDataContext context = this.NewContext;

            this.MakeConcurrentChanges(context);

            try
            {
                context.Connection.Open();
                context.Transaction = context.Connection.BeginTransaction();
                context.SubmitChanges(ConflictMode.ContinueOnConflict);
                context.Transaction.Commit();
            }
            catch (ChangeConflictException)
            {
                context.Transaction.Rollback();
            }
        }
Ejemplo n.º 9
0
        private void MakeConcurrentChanges(LinqBooksDataContext context)
        {
            LinqBooksDataContext context1 = this.NewContext;

            //First user raises the price of each book
            var books1 = context1.Book;

            foreach (var book in books1)
            {
                book.Price += 2;
            }

            //Second user lowers the price of each book
            var books2 = context.Book;

            foreach (var book in books2)
            {
                book.Price -= 1;
            }
            //Go ahead and submit the first changes.
            //The submit using the context passed in to this method will fail.
            context1.SubmitChanges();
        }