コード例 #1
0
        private void SaveChanges_bubbles_exception_during_AcceptChanges_implementation(Func<DbContext, int> saveChanges)
        {
            using (var context = new SimpleModelContext())
            {
                var cat1 = new Category { Id = "AUSSIE FOODS" };
                var cat2 = new Category { Id = "AUSSIE FOODS" };

                context.Categories.Attach(cat1);
                context.Categories.Add(cat2);

                // Accept will fail because of PK violation
                // (cat1 doesn't actually exist in the store so update pipeline will succeed)
                Assert.Throws<InvalidOperationException>(() => context.SaveChanges()).ValidateMessage(
                    "ObjectContext_AcceptAllChangesFailure");
            }
        }
コード例 #2
0
        public void Scenario_Using_two_databases()
        {
            EnsureDatabaseInitialized(() => new LocalDbLoginsContext());
            EnsureDatabaseInitialized(() => new SimpleLocalDbModelContext());

            using (var context = new LocalDbLoginsContext())
            {
                var login = new Login
                    {
                        Id = Guid.NewGuid(),
                        Username = "******"
                    };
                context.Logins.Add(login);
                context.SaveChanges();

                // Scenario ends; simple validation of final state follows
                Assert.Same(login, context.Logins.Find(login.Id));
                Assert.Equal(EntityState.Unchanged, GetStateEntry(context, login).State);

                Assert.Equal(@"(localdb)\v11.0", context.Database.Connection.DataSource);
            }

            using (var context = new SimpleLocalDbModelContext())
            {
                var category = new Category
                    {
                        Id = "Books"
                    };
                var product = new Product
                    {
                        Name = "The Unbearable Lightness of Being",
                        Category = category
                    };
                context.Products.Add(product);
                context.SaveChanges();

                // Scenario ends; simple validation of final state follows
                Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State);
                Assert.Equal(EntityState.Unchanged, GetStateEntry(context, category).State);
                Assert.Equal("Books", product.CategoryId);
                Assert.Same(category, product.Category);
                Assert.True(category.Products.Contains(product));

                Assert.Equal(@"(localdb)\v11.0", context.Database.Connection.DataSource);
            }
        }
コード例 #3
0
        private void SaveChanges_bubbles_exception_during_AcceptChanges_implementation(Func<DbContext, int> saveChanges)
        {
            using (var context = new SimpleModelContext())
            {
                var cat1 = new Category
                {
                    Id = "AUSSIE FOODS"
                };
                var cat2 = new Category
                {
                    Id = "AUSSIE FOODS"
                };

                context.Categories.Attach(cat1);
                context.Categories.Add(cat2);

                // Accept will fail because of PK violation
                // (cat1 doesn't actually exist in the store so update pipeline will succeed)
                var exception = Assert.Throws<InvalidOperationException>(() => saveChanges(context));
                exception.ValidateMessage("ObjectContext_AcceptAllChangesFailure");
                exception.InnerException.ValidateMessage("ObjectStateManager_CannotFixUpKeyToExistingValues", typeof(Category).FullName);
            }
        }
コード例 #4
0
        public void Setting_current_value_of_reference_nav_prop_works_under_partial_trust()
        {
            using (var context = new SimpleModelContext())
            {
                var product = context.Products.Find(1);
                Assert.Null(product.Category);

                var newCategory = new Category("BeanBags");
                context.Entry(product).Reference(p => p.Category).CurrentValue = newCategory;

                Assert.Equal("BeanBags", product.CategoryId);
                Assert.Same(newCategory, product.Category);
            }
        }
コード例 #5
0
        public void Scenario_Using_two_databases()
        {
            EnsureDatabaseInitialized(() => new LoginsContext());
            EnsureDatabaseInitialized(() => new SimpleModelContext());

            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
                {
                    using (new TransactionScope())
                    {
                        using (var context = new LoginsContext())
                        {
                            var login = new Login
                            {
                                Id = Guid.NewGuid(),
                                Username = "******"
                            };
                            context.Logins.Add(login);
                            context.SaveChanges();

                            // Scenario ends; simple validation of final state follows
                            Assert.Same(login, context.Logins.Find(login.Id));
                            Assert.Equal(EntityState.Unchanged, GetStateEntry(context, login).State);
                        }
                    }
                });

            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
                {
                    using (new TransactionScope())
                    {
                        using (var context = new SimpleModelContext())
                        {
                            var category = new Category
                            {
                                Id = "Books"
                            };
                            var product = new Product
                            {
                                Name = "The Unbearable Lightness of Being",
                                Category = category
                            };
                            context.Products.Add(product);
                            context.SaveChanges();

                            // Scenario ends; simple validation of final state follows
                            Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State);
                            Assert.Equal(EntityState.Unchanged, GetStateEntry(context, category).State);
                            Assert.Equal("Books", product.CategoryId);
                            Assert.Same(category, product.Category);
                            Assert.True(category.Products.Contains(product));
                        }
                    }
                });
        }