Exemplo n.º 1
0
        public void Generic_DbEntityEntry_State_calls_DetectChanges_for_detached_unchanged_entities()
        {
            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
            {
                using (var context = new SimpleModelContext())
                {
                    using (new TransactionScope())
                    {
                        var entry = context.Entry(new Product());
                        context.Products.Add(entry.Entity);
                        context.SaveChanges();

                        // Ensure that the entity doesn't have a change tracking proxy
                        Assert.Equal(
                            entry.Entity.GetType(),
                            ObjectContext.GetObjectType(entry.Entity.GetType()));

                        // DetectChanges is called the first time the state is queried for a detached entity
                        Assert.Equal(EntityState.Unchanged, entry.State);

                        entry.Entity.Name = "foo";

                        Assert.Equal(EntityState.Unchanged, entry.State);
                    }
                }
            });
        }
Exemplo n.º 2
0
        public void Generic_DbEntityEntry_can_be_implicitly_converted_to_non_generic_version()
        {
            using (var context = new SimpleModelContext())
            {
                var product = context.Products.Find(1);

                var genericEntry = context.Entry(product);

                NonGenericTestMethod(genericEntry, genericEntry.Entity);
            }
        }
Exemplo n.º 3
0
        // TODO: [Fact(Skip = "SDE Merge - No partial trust yet")]
        public void DbEntityEntry_Member_works_for_collections_under_partial_trust()
        {
            using (var context = new SimpleModelContext())
            {
                var category = context.Categories.First();

                var collection = context.Entry(category).Member <ICollection <Product> >("Products");

                Assert.NotNull(collection);
                Assert.IsType <DbCollectionEntry <Category, Product> >(collection);
            }
        }
Exemplo n.º 4
0
        private void Attempting_to_Attach_by_DbEntityEntry_change_state_throws_if_Attach_not_allowed_implementation(
            EntityState state)
        {
            using (var context = new SimpleModelContext())
            {
                context.Categories.Find("Foods");
                var newCategory = new Category("Foods");
                var entry       = context.Entry(newCategory);

                Assert.Throws <InvalidOperationException>(() => entry.State = state).ValidateMessage(
                    "ObjectStateManager_ObjectStateManagerContainsThisEntityKey");
            }
        }
Exemplo n.º 5
0
        // TODO: [Fact(Skip = "SDE Merge - No partial trust yet")]
        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);
            }
        }
Exemplo n.º 6
0
        private void GenericFindEntryTest(EntityState state)
        {
            using (var context = new SimpleModelContext())
            {
                var product = context.Products.Find(1);
                ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(product, state);

                var entry = context.Entry(product);

                Assert.NotNull(entry);
                Assert.Same(product, entry.Entity);
                Assert.Equal(state, entry.State);
            }
        }
Exemplo n.º 7
0
        public void Generic_DbEntityEntry_can_be_used_to_change_entity_state()
        {
            using (var context = new SimpleModelContext())
            {
                var product = context.Products.Find(1);
                var ose     = GetStateEntry(context, product);

                Assert.Equal(EntityState.Unchanged, ose.State);

                context.Entry(product).State = EntityState.Modified;

                Assert.Equal(EntityState.Modified, ose.State);
            }
        }
Exemplo n.º 8
0
        public void Changing_state_from_Modified_to_Unchanged_does_RejectChanges()
        {
            using (var context = new SimpleModelContext())
            {
                // Setup a modified entity, including a modified FK
                var product = context.Products
                              .Where(p => p.Name == "Marmite")
                              .Include(p => p.Category)
                              .Single();
                var foods     = product.Category;
                var beverages = context.Categories.Find("Beverages");

                var ose = GetStateEntry(context, product);

                var nameProp  = context.Entry(product).Property(p => p.Name);
                var catIdProp = context.Entry(product).Property(p => p.CategoryId);

                nameProp.CurrentValue  = "Magic Unicorn Brew";
                catIdProp.CurrentValue = "Beverages";

                Assert.True(nameProp.IsModified);
                Assert.True(catIdProp.IsModified);
                Assert.Same(beverages, product.Category);
                Assert.Equal(EntityState.Modified, ose.State);

                // Change the state back to Unchanged to reject changes
                context.Entry(product).State = EntityState.Unchanged;

                // Verify the changes have been rejected
                Assert.Equal(EntityState.Unchanged, ose.State);
                Assert.False(nameProp.IsModified);
                Assert.False(catIdProp.IsModified);
                Assert.Equal("Marmite", product.Name);
                Assert.Equal("Foods", product.CategoryId);
                Assert.Same(foods, product.Category);
            }
        }
Exemplo n.º 9
0
        public void DbEntityEntry_State_calls_DetectChanges_for_detached_modified_entities()
        {
            using (var context = new SimpleModelContext())
            {
                using (new TransactionScope())
                {
                    var entry = context.Entry((object)new Product());
                    context.Products.Add((Product)entry.Entity);
                    context.SaveChanges();

                    // Ensure that the entity doesn't have a change tracking proxy
                    Assert.Equal(
                        entry.Entity.GetType(),
                        ObjectContext.GetObjectType(entry.Entity.GetType()));

                    ((Product)entry.Entity).Name = "foo";

                    // DetectChanges is called the first time the state is queried for a detached entity
                    Assert.Equal(EntityState.Modified, entry.State);
                }
            }
        }