public void DbPropertyValues_ToObject_for_an_entity_works_under_partial_trust()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");

                var buildingClone = (Building)context.Entry(building).CurrentValues.ToObject();

                Assert.Equal("Building One", buildingClone.Name);
            }
        }
        public void DbPropertyValues_ToObject_for_a_complex_type_works_under_partial_trust()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");

                var addressClone =
                    (Address)context.Entry(building).CurrentValues.GetValue<DbPropertyValues>("Address").ToObject();

                Assert.Equal("Redmond", addressClone.City);
            }
        }
 private void AnonymousType_join_selecting_one_member()
 {
     using (var context = new AdvancedPatternsMasterContext())
     {
         var query = context.WorkOrders.Select(wo => wo.EmployeeId)
                            .Join(
                                context.Employees.Select(e => e.EmployeeId), a => a, b => b, (a, b) => new
                                    {
                                        a
                                    });
         var sql = query.ToString();
         Assert.True(sql != null);
     }
 }
Пример #4
0
 private void GroupBy_aggregate_pushdown_single_key()
 {
     using (var context = new AdvancedPatternsMasterContext())
     {
         var groupByQuery = from workOrder in context.WorkOrders
                            group new
                                {
                                    workOrder.WorkOrderId,
                                    workOrder.Details
                                } by workOrder.EmployeeId
                            into ordersByEmployeeGroup
                            select new
                                {
                                    EmployeeId = ordersByEmployeeGroup.Key,
                                    OrderCount = ordersByEmployeeGroup.Count(),
                                    MaxOrderId = ordersByEmployeeGroup.Max(o => o.WorkOrderId)
                                };
         var sql = groupByQuery.ToString();
         Assert.True(sql != null && sql.ToUpper().Contains("GROUP BY"));
     }
 }
Пример #5
0
        public void Can_create_non_generic_DbSet_for_abstract_base_type()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var set = context.Set(typeof(Employee));
                Assert.Equal(typeof(Employee), set.ElementType);

                var entity = set.Cast<Employee>().FirstOrDefault();
                Assert.IsAssignableFrom<Employee>(entity);
            }
        }
        public void Setting_IsModified_to_false_for_a_modified_complex_property_rejects_changes_to_that_property()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                entry.ComplexProperty(b => b.Address).CurrentValue = CreateNewAddress();
                Assert.True(entry.Property(b => b.Address).IsModified);

                entry.ComplexProperty(b => b.Address).IsModified = false;

                ValidateBuildingAndNameNotModified(context, building);
            }
        }
        public void IsModified_can_be_set_to_false_when_it_is_currently_false()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);

                entry.Property(b => b.Name).IsModified = false;
                entry.ComplexProperty(b => b.Address).IsModified = false;
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified
                    = false;

                Assert.False(entry.Property(b => b.Name).IsModified);
                Assert.False(entry.ComplexProperty(b => b.Address).IsModified);
                Assert.False(
                    entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).
                        IsModified);
                Assert.Equal(EntityState.Unchanged, entry.State);

                var objectContext = GetObjectContext(context);
                Assert.Equal(0,
                             objectContext.ObjectStateManager.GetObjectStateEntry(building).GetModifiedProperties().
                                 Count());
            }
        }
        public void IsModified_can_be_set_to_true_when_it_is_currently_true()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                entry.Property(b => b.Name).IsModified = true;
                entry.ComplexProperty(b => b.Address).IsModified = true;
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified
                    = true;

                entry.Property(b => b.Name).IsModified = true;
                entry.ComplexProperty(b => b.Address).IsModified = true;
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified
                    = true;

                ValidateModifiedProperties(context, building);
            }
        }
        private void TestPropertyValuePositiveForState(Func<DbPropertyEntry<Building, string>, string> getValue,
                                                       Action<DbPropertyEntry<Building, string>, string> setValue,
                                                       EntityState state)
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                var propEntry = entry.Property(b => b.Name);
                entry.State = state;

                Assert.Equal("Building One", getValue(propEntry));

                setValue(propEntry, "New Building");
                Assert.Equal("New Building", getValue(propEntry));
            }
        }
Пример #10
0
        public void Using_wrong_generic_type_with_nested_complex_string_Property_method_throws()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var propertyEntry = GetBuildingEntry(context).ComplexProperty(b => b.Address);

                Assert.Throws<ArgumentException>(() => propertyEntry.Property<Building>("SiteInfo")).ValidateMessage(
                    "DbEntityEntry_WrongGenericForProp", "SiteInfo", "Address", "Building", "SiteInfo");
            }
        }
Пример #11
0
            Using_base_collection_type_for_navigation_property_derived_from_ICollection_entry_using_Member_throws()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entityEntry = GetBuildingEntry(context);

                // MailRooms is typed as IList<MailRoom> so this should never work
                Assert.Throws<ArgumentException>(() => entityEntry.Member<List<MailRoom>>("MailRooms")).ValidateMessage(
                    "DbEntityEntry_WrongGenericForCollectionNavProp", typeof(List<MailRoom>).ToString(), "MailRooms",
                    typeof(Building).ToString(), typeof(ICollection<MailRoom>).ToString());
            }
        }
Пример #12
0
        public void IsModified_stays_true_for_properties_of_a_complex_property_until_changes_are_rejected_to_all_properties_even_if_the_instance_has_been_changed()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var addressEntry = context.Entry(building).ComplexProperty(b => b.Address);

                var originalAddress = building.Address;
                var newAddress = CloneAddress(originalAddress);
                newAddress.SiteInfo = originalAddress.SiteInfo; // Keep same nested complex instance

                building.Address = newAddress;
                building.Address.City = "Grimsby";

                addressEntry = context.Entry(building).ComplexProperty(b => b.Address);
                AssertStateOfAddressProperties(addressEntry, "Grimsby", "WA", "Clean", isModified: true);

                addressEntry.Property(a => a.City).IsModified = false;
                AssertStateOfAddressProperties(addressEntry, "Redmond", "WA", "Clean", isModified: false);
            }
        }
Пример #13
0
            IsModified_stays_true_for_properties_of_a_complex_property_until_changes_are_rejected_to_all_properties()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");

                building.Address.City = "Grimsby";
                building.Address.State = "UK";
                building.Address.SiteInfo.Environment = "Fishy";

                var addressEntry = context.Entry(building).ComplexProperty(b => b.Address);
                AssertStateOfAddressProperties(addressEntry, "Grimsby", "UK", "Fishy", isModified: true);

                addressEntry.Property(a => a.City).IsModified = false;
                AssertStateOfAddressProperties(addressEntry, "Redmond", "UK", "Fishy", isModified: true);

                addressEntry.Property(a => a.State).IsModified = false;
                AssertStateOfAddressProperties(addressEntry, "Redmond", "WA", "Fishy", isModified: true);

                addressEntry.ComplexProperty(a => a.SiteInfo).Property(s => s.Environment).IsModified = false;
                AssertStateOfAddressProperties(addressEntry, "Redmond", "WA", "Clean", isModified: false);
            }
        }
Пример #14
0
        public void Setting_IsModified_to_false_for_a_nested_property_of_a_modified_complex_property_marks_the_entity_as_Unchanged_if_no_properties_remain_modified()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                entry.Property(b => b.Value).CurrentValue = 100.0M;
                entry.ComplexProperty(b => b.Address).Property(a => a.City).CurrentValue = "Madrid";

                entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified = false;
                Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified);
                Assert.Equal("Redmond", building.Address.City);
                Assert.Equal(EntityState.Modified, entry.State); // Value is still modified

                entry.ComplexProperty(b => b.Address).Property(a => a.City).CurrentValue = "Madrid";
                entry.Property(b => b.Value).IsModified = false;

                entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified = false;
                Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified);
                Assert.Equal("Redmond", building.Address.City);
                Assert.Equal(EntityState.Unchanged, entry.State); // Nothing is modified
            }
        }
Пример #15
0
 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
     AdvancedPatternsMasterContext.SetupModel(modelBuilder);
     base.OnModelCreating(modelBuilder);
 }
Пример #16
0
        public void Using_generic_type_that_is_not_ICollection_to_get_collection_entry_using_Member_throws()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entityEntry = GetBuildingEntry(context);

                // MailRooms is typed as IList<MailRoom> so at first glance it looks like this should work
                // but it doesn't because the returned type must use ICollection.
                Assert.Throws<ArgumentException>(() => entityEntry.Member<IList<MailRoom>>("MailRooms")).ValidateMessage
                    ("DbEntityEntry_WrongGenericForCollectionNavProp", typeof(IList<MailRoom>).ToString(), "MailRooms",
                     typeof(Building).ToString(), typeof(ICollection<MailRoom>).ToString());
            }
        }
Пример #17
0
        private void IsPropertyChangedTest(Action<Building, DbEntityEntry<Building>, ObjectStateEntry> test)
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                var stateEntry = GetObjectContext(context).ObjectStateManager.GetObjectStateEntry(building);

                test(building, entry, stateEntry);
            }
        }
Пример #18
0
        public void Using_wrong_generic_type_for_complex_property_with_string_Member_method_throws()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = GetBuildingEntry(context);

                Assert.Throws<ArgumentException>(() => entry.Member<string>("Address")).ValidateMessage(
                    "DbEntityEntry_WrongGenericForProp", "Address", "Building", "String", "Address");
            }
        }
Пример #19
0
        public void IsPropertyChanged_returns_true_for_scalar_property_that_is_changed_and_marked_as_modified()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                var stateEntry = GetObjectContext(context).ObjectStateManager.GetObjectStateEntry(building);

                entry.Property(b => b.Name).CurrentValue = "Oops I Did It Again!";

                Assert.True(entry.Property(b => b.Name).IsModified);
                Assert.True(stateEntry.IsPropertyChanged("Name"));

                building.Name = "Building Two";
                context.ChangeTracker.DetectChanges();
                building.Name = "Building One";
                context.ChangeTracker.DetectChanges();


                Assert.True(entry.Property(b => b.Name).IsModified);
                Assert.False(stateEntry.IsPropertyChanged("Name"));
            }
        }
Пример #20
0
        public void Original_value_cannot_be_read_or_set_for_an_object_in_the_Added_state()
        {
            EntityState state = EntityState.Added;
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                var propEntry = entry.Property(b => b.Name);

                entry.State = state;

                Assert.Throws<InvalidOperationException>(() => { var _ = propEntry.OriginalValue; }).ValidateMessage(
                    "DbPropertyValues_CannotGetValuesForState", "OriginalValues", state.ToString());
                Assert.Throws<InvalidOperationException>(() => propEntry.OriginalValue = "").ValidateMessage(
                    "DbPropertyValues_CannotGetValuesForState", "OriginalValues", state.ToString());
            }
        }
Пример #21
0
        private void DbPropertyEntryTest(EntityState state, Action<DbEntityEntry<Building>> test)
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(new Building
                                          {
                                              Name = "Building 18",
                                              Address = new Address
                                                        {
                                                            Street = "1 Microsoft Way",
                                                            City = "Redmond",
                                                            State = "WA",
                                                            ZipCode = "98052",
                                                            County = "KING",
                                                            SiteInfo = new SiteInfo { Zone = 2, Environment = "Clean" }
                                                        },
                                              NotInModel = "NotInModel",
                                          });

                entry.State = state;

                test(entry);
            }
        }
Пример #22
0
        public void IsModified_returns_true_only_for_modified_properties()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                building.Name = "New Building";
                building.Address.State = "IA";

                ValidateModifiedProperties(context, building);
            }
        }
Пример #23
0
 private DbEntityEntry<Building> GetBuildingEntry(AdvancedPatternsMasterContext context)
 {
     var building = new Building();
     context.Buildings.Attach(building);
     return context.Entry(building);
 }
Пример #24
0
        private void ValidateModifiedProperties(AdvancedPatternsMasterContext context, Building building)
        {
            var entry = context.Entry(building);

            Assert.Equal(EntityState.Modified, entry.State);
            Assert.True(entry.Property(b => b.Name).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).IsModified);
            Assert.False(entry.Property(b => b.Value).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).Property(a => a.Street).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).Property(a => a.State).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).Property(a => a.ZipCode).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).IsModified);
            Assert.True(
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Environment).
                    IsModified);
            Assert.True(
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified);

            var objectContext = GetObjectContext(context);
            var modified = objectContext.ObjectStateManager.GetObjectStateEntry(building).GetModifiedProperties();
            Assert.Equal(2, modified.Count());
            Assert.True(modified.Contains("Name"));
            Assert.True(modified.Contains("Address"));
        }
Пример #25
0
        public void Using_Collection_for_a_complex_prop_throws()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = GetBuildingEntry(context);

                Assert.Throws<ArgumentException>(() => entry.Collection("Address")).ValidateMessage(
                    "DbEntityEntry_NotANavigationProperty", "Address", "Building");
            }
        }
Пример #26
0
        private void ValidateBuildingAndNameNotModified(AdvancedPatternsMasterContext context, Building building)
        {
            var entry = context.Entry(building);

            Assert.False(entry.Property(b => b.Name).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.Street).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.State).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.ZipCode).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).IsModified);
            Assert.False(
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Environment).
                    IsModified);
            Assert.False(
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified);

            Assert.Equal("Building One", entry.Property(b => b.Name).CurrentValue);
            Assert.Equal("100 Work St", entry.ComplexProperty(b => b.Address).Property(a => a.Street).CurrentValue);
            Assert.Equal("Redmond", entry.ComplexProperty(b => b.Address).Property(a => a.City).CurrentValue);
            Assert.Equal("WA", entry.ComplexProperty(b => b.Address).Property(a => a.State).CurrentValue);
            Assert.Equal("98052", entry.ComplexProperty(b => b.Address).Property(a => a.ZipCode).CurrentValue);
            Assert.Equal("Clean",
                         entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(
                             i => i.Environment).CurrentValue);
            Assert.Equal(1,
                         entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).
                             CurrentValue);

            var objectContext = GetObjectContext(context);
            var modified = objectContext.ObjectStateManager.GetObjectStateEntry(building).GetModifiedProperties();
            Assert.False(modified.Contains("Name"));
            Assert.False(modified.Contains("Address"));
        }
Пример #27
0
        public void Can_get_generic_complex_entry_using_Member()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entityEntry = GetBuildingEntry(context);

                var propEntry = entityEntry.Member<Address>("Address");

                Assert.IsType<DbComplexPropertyEntry<Building, Address>>(propEntry);
                Assert.Same(entityEntry.Entity.Address, propEntry.CurrentValue);
            }
        }
Пример #28
0
        public void Can_create_DbSet_for_abstract_base_type()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var set = context.Set<Employee>();
                Assert.IsType<DbSet<Employee>>(set);

                var entity = set.FirstOrDefault();
                Assert.IsAssignableFrom<Employee>(entity);
            }
        }
Пример #29
0
        public void Can_get_non_complex_entry_using_Member()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entityEntry = (DbEntityEntry)GetBuildingEntry(context);

                var propEntry = entityEntry.Member("Address");

                Assert.IsType<DbComplexPropertyEntry>(propEntry);
                Assert.Same(((Building)entityEntry.Entity).Address, propEntry.CurrentValue);
            }
        }
Пример #30
0
        private void SaveChanges_bubbles_presave_exception_implementation(Func<DbContext, int> saveChanges)
        {
            EnsureDatabaseInitialized(() => new AdvancedPatternsMasterContext());

            using (new TransactionScope())
            {
                using (var context = new AdvancedPatternsMasterContext())
                {
                    var emp = new CurrentEmployee { EmployeeId = 4 };
                    var ord = new WorkOrder { WorkOrderId = 2, EmployeeId = 4 };
                    context.Employees.Attach(emp);
                    context.WorkOrders.Attach(ord);

                    // Create a conceptual null
                    GetObjectContext(context).ObjectStateManager.ChangeObjectState(emp, EntityState.Deleted);

                    Assert.Throws<InvalidOperationException>(() => context.SaveChanges()).ValidateMessage(
                        "ObjectContext_CommitWithConceptualNull");
                }
            }
        }
Пример #31
0
        public void Can_get_generic_collection_for_navigation_property_derived_from_ICollection_entry_using_Member()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entityEntry = GetBuildingEntry(context);

                var propEntry = entityEntry.Member<ICollection<MailRoom>>("MailRooms");

                Assert.IsType<DbCollectionEntry<Building, MailRoom>>(propEntry);
                Assert.Same(entityEntry.Entity.MailRooms, propEntry.CurrentValue);
            }
        }