Esempio n. 1
0
        // TODO: [Fact(Skip = "SDE Merge - No partial trust yet")]
        public void DbPropertyValues_SetValues_for_an_entity_wih_complex_objects_works_under_partial_trust()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");

                var newBuilding = new Building
                {
                    BuildingId = new Guid(building.BuildingId.ToString()),
                    Name       = "Bag End",
                    Value      = building.Value,
                    Address    = new Address
                    {
                        Street   = "The Hill",
                        City     = "Hobbiton",
                        State    = "WF",
                        ZipCode  = "00001",
                        SiteInfo = new SiteInfo
                        {
                            Zone        = 3,
                            Environment = "Comfortable"
                        }
                    },
                };

                context.Entry(building).CurrentValues.SetValues(newBuilding);

                Assert.Equal("Bag End", building.Name);
                Assert.Equal("Hobbiton", building.Address.City);
                Assert.Equal("Comfortable", building.Address.SiteInfo.Environment);
            }
        }
        public void Multiple_entities_in_exception_from_update_pipeline_can_be_handled()
        {
            ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                () =>
            {
                using (new TransactionScope())
                {
                    using (var context = new AdvancedPatternsMasterContext())
                    {
                        // Create two entities which both have dependencies on each other
                        // to force a dependency ordering exception from the update pipeline.

                        var building = new Building
                        {
                            BuildingId          = new Guid("14C62AB6-A49C-40BD-BD5C-D374E070D3D7"),
                            Name                = "Building 18",
                            Value               = 1m,
                            PrincipalMailRoomId = -1,
                            Address             =
                                new Address
                            {
                                Street   = "100 Work St",
                                City     = "Redmond",
                                State    = "WA",
                                ZipCode  = "98052",
                                SiteInfo = new SiteInfo
                                {
                                    Zone        = 1,
                                    Environment = "Clean"
                                }
                            },
                        };

                        var mailRoom = new MailRoom
                        {
                            id         = (int)building.PrincipalMailRoomId,
                            BuildingId = building.BuildingId
                        };

                        context.Buildings.Add(building);
                        context.Set <MailRoom>().Add(mailRoom);

                        try
                        {
                            context.SaveChanges();
                            Assert.True(false);
                        }
                        catch (DbUpdateException ex)
                        {
                            Assert.IsType <UpdateException>(ex.InnerException);

                            var entries = ex.Entries.ToList();
                            Assert.Equal(2, entries.Count());
                            Assert.True(entries.Any(e => ReferenceEquals(e.Entity, building)));
                            Assert.True(entries.Any(e => ReferenceEquals(e.Entity, mailRoom)));
                        }
                    }
                }
            });
        }
        Calling_DetectChanges_twice_for_nested_complex_type_that_was_null_but_is_no_longer_null_should_work_and_original_value_should_be_null
            ()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(CreateBuilding(nullAddress: false, nullSiteInfo: true));
                entry.State = EntityState.Unchanged;

                context.ChangeTracker.DetectChanges();

                entry.Entity.Address.SiteInfo = CreateSiteInfo();

                context.ChangeTracker.DetectChanges();
                context.ChangeTracker.DetectChanges();

                Assert.NotNull(entry.Property(b => b.Address).OriginalValue);
                Assert.NotNull(entry.Property(b => b.Address).CurrentValue);
                Assert.Equal("Donkey Boulevard", entry.Property(b => b.Address).OriginalValue.Street);
                Assert.Equal("Donkey Boulevard", entry.Property(b => b.Address).CurrentValue.Street);

                Assert.Null(entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).OriginalValue);
                Assert.NotNull(entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).CurrentValue);
                Assert.Equal(18, entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).CurrentValue.Zone);
            }
        }
Esempio n. 4
0
        private AdvancedPatternsMasterContext AdvancedContextWithNoLazyLoading()
        {
            var context = new AdvancedPatternsMasterContext();

            context.Configuration.LazyLoadingEnabled = false;
            return(context);
        }
Esempio n. 5
0
 public void Creating_DbEntityEntry_for_complex_type_throws()
 {
     using (var context = new AdvancedPatternsMasterContext())
     {
         Assert.Throws <InvalidOperationException>(() => context.Entry(new Address())).ValidateMessage(
             "DbSet_DbSetUsedWithComplexType", typeof(Address).Name);
     }
 }
Esempio n. 6
0
 public void Non_generic_Set_throws_only_when_used_if_type_is_unmapped_base_type()
 {
     using (var ctx = new AdvancedPatternsMasterContext())
     {
         var set = ctx.Set(typeof(UnMappedPersonBase));
         Assert.Throws <InvalidOperationException>(() => set.Cast <UnMappedPersonBase>().FirstOrDefault()).
         ValidateMessage("DbSet_EntityTypeNotInModel", typeof(UnMappedPersonBase).Name);
     }
 }
Esempio n. 7
0
 public void Non_generic_Attach_throws_when_type_is_unmapped_derived_entity()
 {
     using (var context = new AdvancedPatternsMasterContext())
     {
         var office = new UnMappedOffice();
         Assert.Throws <InvalidOperationException>(() => context.Set(typeof(Office)).Attach(office)).
         ValidateMessage("ObjectContext_NoMappingForEntityType", typeof(UnMappedOffice).FullName);
     }
 }
Esempio n. 8
0
 public void Non_generic_Set_throws_only_when_used_if_type_is_a_complex_type_Dev10_885806()
 {
     using (var ctx = new AdvancedPatternsMasterContext())
     {
         var set = ctx.Set(typeof(Address));
         Assert.Throws <InvalidOperationException>(() => set.Cast <Address>().FirstOrDefault()).ValidateMessage(
             "DbSet_DbSetUsedWithComplexType", typeof(Address).Name);
     }
 }
Esempio n. 9
0
        // TODO: [Fact(Skip = "SDE Merge - No partial trust yet")]
        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);
            }
        }
Esempio n. 10
0
        // TODO: [Fact(Skip = "SDE Merge - No partial trust yet")]
        public void Non_generic_DbSet_Create_works_under_partial_trust()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Set(typeof(Building)).Create(typeof(Building));

                Assert.NotNull(building);
                Assert.IsAssignableFrom <Building>(building);
                Assert.IsNotType <Building>(building);
            }
        }
            public override DbContext CreateContext(NpgsqlTestStore testStore)
            {
                var optionsBuilder = new DbContextOptionsBuilder()
                                     .UseNpgsql(testStore.Connection, b => b.ApplyConfiguration())
                                     .UseInternalServiceProvider(_serviceProvider);

                var context = new AdvancedPatternsMasterContext(optionsBuilder.Options);

                context.Database.UseTransaction(testStore.Transaction);

                return(context);
            }
Esempio n. 12
0
        static ConcurrencyTests()
        {
            using (var context = new F1Context())
            {
                context.Database.Initialize(force: false);
            }

            using (var context = new AdvancedPatternsMasterContext())
            {
                context.Database.Initialize(force: false);
            }
        }
Esempio n. 13
0
        // TODO: [Fact(Skip = "SDE Merge - No partial trust yet")]
        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);
            }
        }
Esempio n. 14
0
            public override DbContext CreateContext(SqliteTestStore testStore)
            {
                var optionsBuilder = new DbContextOptionsBuilder()
                                     .UseSqlite(testStore.Connection)
                                     .UseInternalServiceProvider(_serviceProvider);

                var context = new AdvancedPatternsMasterContext(optionsBuilder.Options);

                context.Database.UseTransaction(testStore.Transaction);

                return(context);
            }
Esempio n. 15
0
        private void SQL_query_can_be_used_to_materialize_complex_types_implementation(
            Func <AdvancedPatternsMasterContext, string, List <SiteInfo> > query)
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var siteInfos = query(
                    context,
                    "select Address_SiteInfo_Zone as Zone, Address_SiteInfo_Environment as Environment from Buildings");

                Assert.Equal(2, siteInfos.Count);
            }
        }
Esempio n. 16
0
 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);
     }
 }
            public override NpgsqlTestStore CreateTestStore()
            => NpgsqlTestStore.GetOrCreateShared(DatabaseName, () =>
            {
                var optionsBuilder = new DbContextOptionsBuilder()
                                     .UseNpgsql(NpgsqlTestStore.CreateConnectionString(DatabaseName), b => b.ApplyConfiguration())
                                     .UseInternalServiceProvider(_serviceProvider);

                using (var context = new AdvancedPatternsMasterContext(optionsBuilder.Options))
                {
                    context.Database.EnsureCreated();
                    Seed(context);
                }
            });
        public void Setting_a_nested_complex_property_current_value_onto_a_complex_property_that_is_null_for_a_Detached_entity_throws()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(CreateBuilding(nullAddress: true));

                Assert.Throws <InvalidOperationException>(
                    () =>
                    entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).CurrentValue = CreateSiteInfo()).
                ValidateMessage(
                    "DbPropertyValues_CannotSetPropertyOnNullCurrentValue", typeof(SiteInfo).Name,
                    typeof(Address).Name);
            }
        }
Esempio n. 19
0
            public override SqliteTestStore CreateTestStore()
            {
                return(SqliteTestStore.GetOrCreateShared(DatabaseName, () =>
                {
                    var optionsBuilder = new DbContextOptionsBuilder()
                                         .UseSqlite(SqliteTestStore.CreateConnectionString(DatabaseName))
                                         .UseInternalServiceProvider(_serviceProvider);

                    using (var context = new AdvancedPatternsMasterContext(optionsBuilder.Options))
                    {
                        context.Database.EnsureClean();
                        Seed(context);
                    }
                }));
            }
        Calling_DetectChanges_twice_for_complex_type_that_is_null_should_work_and_original_value_should_be_null()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(CreateBuilding(nullAddress: true));
                entry.State = EntityState.Unchanged;

                context.ChangeTracker.DetectChanges();
                context.ChangeTracker.DetectChanges();

                Assert.Null(entry.Property(b => b.Address).OriginalValue);
                Assert.Null(entry.Property(b => b.Address).CurrentValue);

                Assert.Null(entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).OriginalValue);
                Assert.Null(entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).CurrentValue);
            }
        }
        Setting_a_nested_complex_property_original_value_onto_a_complex_property_that_was_originally_null_throws()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(CreateBuilding(nullAddress: true));
                entry.State = EntityState.Unchanged;

                entry.Entity.Address = CreateAddress(nullSiteInfo: true);
                context.ChangeTracker.DetectChanges();

                Assert.Null(entry.Property(b => b.Address).OriginalValue);

                Assert.Throws <InvalidOperationException>(
                    () =>
                    entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).OriginalValue = CreateSiteInfo()).
                ValidateMessage(
                    "DbPropertyValues_CannotSetPropertyOnNullOriginalValue", typeof(SiteInfo).Name,
                    typeof(Address).Name);
            }
        }
Esempio n. 22
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"));
     }
 }
Esempio n. 23
0
        public void Set_throws_only_when_used_if_type_derives_from_valid_type()
        {
            // Create a derived type in a new assembly
            var provider = new CSharpCodeProvider();
            var result   = provider.CompileAssemblyFromSource(
                new CompilerParameters(new[] { GetType().Assembly.Location }),
                "public class DerivedCategory : SimpleModel.Category { }");

            var derivedCategoryType           = result.CompiledAssembly.GetTypes().Single();
            var dbContextSet                  = typeof(DbContext).GetMethod("Set", Type.EmptyTypes);
            var dbContextSetOfDerivedCategory = dbContextSet.MakeGenericMethod(derivedCategoryType);

            using (var ctx = new AdvancedPatternsMasterContext())
            {
                // Create a set for the new type
                var set = dbContextSetOfDerivedCategory.Invoke(ctx, null);
                var iQueryableFirstOrDefault = typeof(Queryable)
                                               .GetMethods()
                                               .Single(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 1);
                var iQueryableOfDerivedCategoryFirstOrDefault =
                    iQueryableFirstOrDefault.MakeGenericMethod(derivedCategoryType);

                // Attempt to use the set
                try
                {
                    iQueryableOfDerivedCategoryFirstOrDefault.Invoke(null, new[] { set });
                }
                catch (TargetInvocationException ex)
                {
                    Assert.IsType <InvalidOperationException>(ex.InnerException);
                    new StringResourceVerifier(
                        new AssemblyResourceLookup(
                            typeof(DbModelBuilder).Assembly,
                            "System.Data.Entity.Properties.Resources"))
                    .VerifyMatch(
                        "DbSet_EntityTypeNotInModel", ex.InnerException.Message,
                        new[] { "DerivedCategory" });
                }
            }
        }