Пример #1
0
        public void Can_detect_added_modification_functions()
        {
            var modelBuilder = new DbModelBuilder();

            var model1 = modelBuilder.Build(ProviderInfo);

            var model2 = new TestContext();

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel());

            var createProcedureOperations
                = new EdmModelDiffer()
                  .Diff(
                      model1.GetModel(),
                      model2.GetModel(),
                      commandTreeGenerator,
                      new SqlServerMigrationSqlGenerator())
                  .OfType <CreateProcedureOperation>()
                  .ToList();

            Assert.Equal(14, createProcedureOperations.Count);
            Assert.True(createProcedureOperations.All(c => c.Name.Any()));
            Assert.True(createProcedureOperations.All(c => c.BodySql.Any()));
        }
        public void Can_convert_delete_command_trees_when_many_to_many()
        {
            var model = TestContext.CreateDynamicUpdateModel();

            var modificationFunctionMapping
                = TestContext.GetAssociationModificationFunctionMapping("OrderThing_Orders");

            var converter
                = new DynamicToFunctionModificationCommandConverter(
                      modificationFunctionMapping.Item1, modificationFunctionMapping.Item2);

            var modificationCommandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = modificationCommandTreeGenerator
                  .GenerateAssociationDelete(modificationFunctionMapping.Item1.AssociationSet.ElementType.FullName);

            Assert.Equal(1, commandTrees.Count());

            var resultTrees = converter.Convert(commandTrees);

            Assert.Equal(1, resultTrees.Count());

            var commandTree = resultTrees.First();

            Assert.Equal(5, commandTree.Parameters.Count());

            Assert.Equal("order_thing_id", commandTree.Parameters.ElementAt(0).Key);
            Assert.Equal("Order_Id", commandTree.Parameters.ElementAt(1).Key);
            Assert.Equal("Order_Key", commandTree.Parameters.ElementAt(2).Key);
            Assert.Equal("teh_codez_bro", commandTree.Parameters.ElementAt(3).Key);
            Assert.Equal("Order_Signature", commandTree.Parameters.ElementAt(4).Key);
            Assert.NotNull(commandTree.Predicate);
        }
Пример #3
0
        private object InstantiateEntity(
            EntityType entityType,
            DbContext context,
            Type clrType,
            DbSet set)
        {
            object structuralObject;

            if (!clrType.IsAbstract())
            {
                structuralObject = set.Create();
            }
            else
            {
                EntityType entityType1 = this._metadataWorkspace.GetItems <EntityType>(DataSpace.CSpace).First <EntityType>((Func <EntityType, bool>)(et =>
                {
                    if (entityType.IsAncestorOf(et))
                    {
                        return(!et.Abstract);
                    }
                    return(false);
                }));
                structuralObject = context.Set(EntityTypeExtensions.GetClrType(entityType1)).Create();
            }
            ModificationCommandTreeGenerator.InstantiateComplexProperties(structuralObject, (IEnumerable <EdmProperty>)entityType.Properties);
            return(structuralObject);
        }
        public void Can_generate_update_tree_when_table_splitting_dependent()
        {
            DbModel model;

            using (var context = new TableSplittingContext())
            {
                model
                    = context
                      .InternalContext
                      .CodeFirstModel
                      .CachedModelBuilder
                      .BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
            }

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = commandTreeGenerator
                  .GenerateUpdate(GetType().Namespace + ".StandingStone")
                  .ToList();

            Assert.Equal(1, commandTrees.Count());
            Assert.IsType <DbUpdateCommandTree>(commandTrees.Single());
        }
        public void Can_convert_insert_command_trees_when_table_splitting()
        {
            var model = TestContext.CreateDynamicUpdateModel();

            var modificationFunctionMapping
                = TestContext.GetModificationFunctionMapping("JobTask");

            var converter
                = new DynamicToFunctionModificationCommandConverter(
                      modificationFunctionMapping.Item1, modificationFunctionMapping.Item2);

            var modificationCommandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = modificationCommandTreeGenerator
                  .GenerateInsert(modificationFunctionMapping.Item1.EntityType.FullName)
                  .ToList();

            Assert.Equal(1, commandTrees.Count());

            var resultTrees = converter.Convert(commandTrees).ToList();

            Assert.Equal(1, resultTrees.Count());

            var firstCommandTree = (DbUpdateCommandTree)resultTrees.First();

            Assert.Equal(2, firstCommandTree.Parameters.Count());
        }
Пример #6
0
 private static void SetFakeKeyValues(object entity, EntityType entityType)
 {
     foreach (EdmProperty keyProperty in entityType.KeyProperties)
     {
         PropertyInfo clrPropertyInfo = keyProperty.GetClrPropertyInfo();
         object       fakeKeyValue    = ModificationCommandTreeGenerator.GetFakeKeyValue(keyProperty.UnderlyingPrimitiveType.PrimitiveTypeKind);
         clrPropertyInfo.GetPropertyInfoForSet().SetValue(entity, fakeKeyValue, (object[])null);
     }
 }
Пример #7
0
        private object InstantiateAndAttachEntity(EntityType entityType, DbContext context)
        {
            Type   clrType = EntityTypeExtensions.GetClrType(entityType);
            DbSet  set     = context.Set(clrType);
            object entity  = this.InstantiateEntity(entityType, context, clrType, set);

            ModificationCommandTreeGenerator.SetFakeReferenceKeyValues(entity, entityType);
            ModificationCommandTreeGenerator.SetFakeKeyValues(entity, entityType);
            set.Attach(entity);
            return(entity);
        }
Пример #8
0
        private void ChangeRelationshipStates(
            DbContext context,
            EntityType entityType,
            object entity,
            EntityState state)
        {
            ObjectStateManager objectStateManager = ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager;

            foreach (AssociationType associationType in this._metadataWorkspace.GetItems <AssociationType>(DataSpace.CSpace).Where <AssociationType>((Func <AssociationType, bool>)(at =>
            {
                if (at.IsForeignKey || at.IsManyToMany())
                {
                    return(false);
                }
                if (!at.SourceEnd.GetEntityType().IsAssignableFrom((EdmType)entityType))
                {
                    return(at.TargetEnd.GetEntityType().IsAssignableFrom((EdmType)entityType));
                }
                return(true);
            })))
            {
                AssociationEndMember principalEnd;
                AssociationEndMember dependentEnd;
                if (!associationType.TryGuessPrincipalAndDependentEnds(out principalEnd, out dependentEnd))
                {
                    principalEnd = associationType.SourceEnd;
                    dependentEnd = associationType.TargetEnd;
                }
                if (dependentEnd.GetEntityType().IsAssignableFrom((EdmType)entityType))
                {
                    EntityType entityType1 = principalEnd.GetEntityType();
                    Type       clrType     = EntityTypeExtensions.GetClrType(entityType1);
                    DbSet      set         = context.Set(clrType);
                    object     obj1        = set.Local.Cast <object>().SingleOrDefault <object>();
                    if (obj1 == null || object.ReferenceEquals(entity, obj1) && state == EntityState.Added)
                    {
                        obj1 = this.InstantiateEntity(entityType1, context, clrType, set);
                        ModificationCommandTreeGenerator.SetFakeReferenceKeyValues(obj1, entityType1);
                        set.Attach(obj1);
                    }
                    if (principalEnd.IsRequired() && state == EntityState.Modified)
                    {
                        object obj2 = this.InstantiateEntity(entityType1, context, clrType, set);
                        ModificationCommandTreeGenerator.SetFakeKeyValues(obj2, entityType1);
                        set.Attach(obj2);
                        objectStateManager.ChangeRelationshipState(entity, obj2, associationType.FullName, principalEnd.Name, EntityState.Deleted);
                    }
                    objectStateManager.ChangeRelationshipState(entity, obj1, associationType.FullName, principalEnd.Name, state == EntityState.Deleted ? state : EntityState.Added);
                }
            }
        }
Пример #9
0
 private static void InstantiateComplexProperties(
     object structuralObject,
     IEnumerable <EdmProperty> properties)
 {
     foreach (EdmProperty property in properties)
     {
         if (property.IsComplexType)
         {
             PropertyInfo clrPropertyInfo = property.GetClrPropertyInfo();
             object       instance        = Activator.CreateInstance(clrPropertyInfo.PropertyType);
             ModificationCommandTreeGenerator.InstantiateComplexProperties(instance, (IEnumerable <EdmProperty>)property.ComplexType.Properties);
             clrPropertyInfo.GetPropertyInfoForSet().SetValue(structuralObject, instance, (object[])null);
         }
     }
 }
Пример #10
0
        public void Can_generate_update_tree_when_self_ref_inheritance()
        {
            DbModel model;

            using (var context = new SelfRefInheritanceContext())
            {
                model = context.InternalContext.CodeFirstModel.CachedModelBuilder.BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
            }

            var commandTreeGenerator = new ModificationCommandTreeGenerator(model);

            var commandTrees = commandTreeGenerator.GenerateUpdate(GetType().Namespace + ".Comment").ToList();

            Assert.Equal(1, commandTrees.Count());
        }
Пример #11
0
        public void Can_generate_delete_tree_when_self_ref_direct()
        {
            DbModel model;

            using (var context = new WorldContext_Identity())
            {
                model = context.InternalContext.CodeFirstModel.CachedModelBuilder.BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
            }

            var commandTreeGenerator = new ModificationCommandTreeGenerator(model);

            var commandTrees = commandTreeGenerator.GenerateDelete(GetType().Namespace + ".Thing").ToList();

            Assert.Equal(1, commandTrees.Count());
        }
        public void Can_convert_update_command_trees()
        {
            var model = TestContext.CreateDynamicUpdateModel();

            var modificationFunctionMapping
                = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder");

            var converter
                = new DynamicToFunctionModificationCommandConverter(
                      modificationFunctionMapping.Item1, modificationFunctionMapping.Item2);

            var modificationCommandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = modificationCommandTreeGenerator
                  .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName);

            Assert.Equal(3, commandTrees.Count());

            var resultTrees = converter.Convert(commandTrees);

            Assert.Equal(3, resultTrees.Count());

            var firstCommandTree = (DbUpdateCommandTree)resultTrees.First();

            Assert.Equal(12, firstCommandTree.Parameters.Count());
            Assert.Equal(6, firstCommandTree.SetClauses.Count());

            Assert.Equal("Name", firstCommandTree.Parameters.ElementAt(0).Key);
            Assert.Equal("Address_Street", firstCommandTree.Parameters.ElementAt(1).Key);
            Assert.Equal("Address_City", firstCommandTree.Parameters.ElementAt(2).Key);
            Assert.Equal("Address_CountryOrRegion_Name", firstCommandTree.Parameters.ElementAt(3).Key);
            Assert.Equal("OrderGroupId", firstCommandTree.Parameters.ElementAt(4).Key);
            Assert.Equal("Customer_CustomerId", firstCommandTree.Parameters.ElementAt(5).Key);
            Assert.Equal("xid", firstCommandTree.Parameters.ElementAt(6).Key);
            Assert.Equal("key_for_update", firstCommandTree.Parameters.ElementAt(7).Key);
            Assert.Equal("Code", firstCommandTree.Parameters.ElementAt(8).Key);
            Assert.Equal("Signature", firstCommandTree.Parameters.ElementAt(9).Key);
            Assert.Equal("Name_Original", firstCommandTree.Parameters.ElementAt(10).Key);
            Assert.Equal("RowVersion_Original", firstCommandTree.Parameters.ElementAt(11).Key);

            var properties = ((RowType)firstCommandTree.Returning.ResultType.EdmType).Properties;

            Assert.Equal(2, properties.Count);
            Assert.Equal("order_fu", properties[0].Name);
        }
        public void Can_generate_dynamic_delete_command_trees_for_many_to_many_association()
        {
            var model = TestContext.CreateDynamicUpdateModel();

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = commandTreeGenerator
                  .GenerateAssociationDelete(GetType().Namespace + ".FunctionsModel.OrderThing_Orders")
                  .ToList();

            Assert.Equal(1, commandTrees.Count());

            var commandTree = commandTrees.First();

            Assert.Equal("OrderThingOrder", commandTree.Target.VariableType.EdmType.Name);
            Assert.NotNull(commandTree.Predicate);
        }
        public void Can_convert_insert_command_trees()
        {
            var model = TestContext.CreateDynamicUpdateModel();

            var modificationFunctionMapping
                = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder");

            var converter
                = new DynamicToFunctionModificationCommandConverter(
                      modificationFunctionMapping.Item1, modificationFunctionMapping.Item2);

            var modificationCommandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = modificationCommandTreeGenerator
                  .GenerateInsert(modificationFunctionMapping.Item1.EntityType.FullName);

            Assert.Equal(3, commandTrees.Count());

            var resultTrees = converter.Convert(commandTrees);

            Assert.Equal(3, resultTrees.Count());

            var firstCommandTree = (DbInsertCommandTree)resultTrees.First();

            Assert.Equal(8, firstCommandTree.Parameters.Count());
            Assert.Equal(8, firstCommandTree.SetClauses.Count());

            Assert.Equal("teh_codez", firstCommandTree.Parameters.ElementAt(0).Key);
            Assert.Equal("Signature", firstCommandTree.Parameters.ElementAt(1).Key);
            Assert.Equal("the_name", firstCommandTree.Parameters.ElementAt(2).Key);
            Assert.Equal("Address_Street", firstCommandTree.Parameters.ElementAt(3).Key);
            Assert.Equal("Address_City", firstCommandTree.Parameters.ElementAt(4).Key);
            Assert.Equal("Address_CountryOrRegion_Name", firstCommandTree.Parameters.ElementAt(5).Key);
            Assert.Equal("OrderGroupId", firstCommandTree.Parameters.ElementAt(6).Key);
            Assert.Equal("Customer_CustomerId", firstCommandTree.Parameters.ElementAt(7).Key);

            var properties = ((RowType)firstCommandTree.Returning.ResultType.EdmType).Properties;

            Assert.Equal(4, properties.Count);
            Assert.Equal("key_result", properties[1].Name);
        }
        public void Can_generate_dynamic_update_command_trees()
        {
            var model = TestContext.CreateDynamicUpdateModel();

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = commandTreeGenerator
                  .GenerateUpdate(GetType().Namespace + ".FunctionsModel.SpecialOrder")
                  .ToList();

            Assert.Equal(2, commandTrees.Count());

            var commandTree = (DbUpdateCommandTree)commandTrees.First();

            Assert.Equal(6, commandTree.SetClauses.Count);
            Assert.NotNull(commandTree.Predicate);
            Assert.NotNull(commandTree.Returning);
            Assert.Equal("Order", commandTree.Target.VariableType.EdmType.Name);

            commandTree = (DbUpdateCommandTree)commandTrees.Last();

            Assert.Equal(4, commandTree.SetClauses.Count);
            Assert.NotNull(commandTree.Predicate);
            Assert.NotNull(commandTree.Returning);
            Assert.Equal("special_orders", commandTree.Target.VariableType.EdmType.Name);

            commandTrees
                = commandTreeGenerator
                  .GenerateUpdate(GetType().Namespace + ".FunctionsModel.Customer")
                  .ToList();

            Assert.Equal(1, commandTrees.Count());

            commandTree = (DbUpdateCommandTree)commandTrees.Single();

            Assert.Equal(1, commandTree.SetClauses.Count);
            Assert.Equal("Customer", commandTree.Target.VariableType.EdmType.Name);
            Assert.Null(commandTree.Returning);
        }
        public void Can_generate_update_tree_when_one_to_one_ia()
        {
            DbModel model;

            using (var context = new GearsOfWarContextSPBug())
            {
                model
                    = context
                      .InternalContext
                      .CodeFirstModel
                      .CachedModelBuilder
                      .BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
            }

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = commandTreeGenerator
                  .GenerateUpdate(GetType().Namespace + ".Gear")
                  .ToList();

            Assert.Equal(1, commandTrees.Count());
        }
        public void Can_generate_delete_association_tree_when_many_to_many_with_abstract_end()
        {
            DbModel model;

            using (var context = new GearsModelManyToMany())
            {
                model
                    = context
                      .InternalContext
                      .CodeFirstModel
                      .CachedModelBuilder
                      .BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
            }

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = commandTreeGenerator
                  .GenerateAssociationDelete(GetType().Namespace + ".GearBase_Weapons")
                  .ToList();

            Assert.Equal(1, commandTrees.Count());
        }
        public void Can_generate_delete_tree_when_one_to_one_self_ref()
        {
            DbModel model;

            using (var context = new GearsModelOneToOneSelfRef())
            {
                model
                    = context
                      .InternalContext
                      .CodeFirstModel
                      .CachedModelBuilder
                      .BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
            }

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = commandTreeGenerator
                  .GenerateDelete(GetType().Namespace + ".WeaponBase")
                  .ToList();

            Assert.Equal(1, commandTrees.Count());
        }
        public void Can_generate_delete_association_tree_when_many_to_many_self_ref()
        {
            DbModel model;

            using (var context = new ManyToManySelfRef())
            {
                model
                    = context
                      .InternalContext
                      .CodeFirstModel
                      .CachedModelBuilder
                      .BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
            }

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = commandTreeGenerator
                  .GenerateAssociationDelete(GetType().Namespace + ".ArubaPerson_Children")
                  .ToList();

            Assert.Equal(1, commandTrees.Count());
        }
        public void Can_generate_insert_tree_when_one_to_one_fk_principal()
        {
            DbModel model;

            using (var context = new OneToOneFkContext())
            {
                model
                    = context
                      .InternalContext
                      .CodeFirstModel
                      .CachedModelBuilder
                      .BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
            }

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = commandTreeGenerator
                  .GenerateInsert(GetType().Namespace + ".Landmark")
                  .ToList();

            Assert.Equal(1, commandTrees.Count());
        }
        public void Can_generate_delete_tree_when_ia_required_to_many()
        {
            DbModel model;

            using (var context = new ArubaContext())
            {
                model
                    = context
                      .InternalContext
                      .CodeFirstModel
                      .CachedModelBuilder
                      .BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
            }

            var commandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = commandTreeGenerator
                  .GenerateDelete(GetType().Namespace + ".ArubaTask")
                  .ToList();

            Assert.Equal(1, commandTrees.Count());
        }
        public void Can_convert_delete_command_trees()
        {
            var model = TestContext.CreateDynamicUpdateModel();

            var modificationFunctionMapping
                = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder");

            var converter
                = new DynamicToFunctionModificationCommandConverter(
                      modificationFunctionMapping.Item1, modificationFunctionMapping.Item2);

            var modificationCommandTreeGenerator
                = new ModificationCommandTreeGenerator(model);

            var commandTrees
                = modificationCommandTreeGenerator
                  .GenerateDelete(modificationFunctionMapping.Item1.EntityType.FullName);

            Assert.Equal(3, commandTrees.Count());

            var resultTrees = converter.Convert(commandTrees);

            Assert.Equal(3, resultTrees.Count());

            var lastCommandTree = resultTrees.Last();

            Assert.Equal(7, lastCommandTree.Parameters.Count());

            Assert.Equal("xid", lastCommandTree.Parameters.ElementAt(0).Key);
            Assert.Equal("key_for_delete", lastCommandTree.Parameters.ElementAt(1).Key);
            Assert.Equal("Code", lastCommandTree.Parameters.ElementAt(2).Key);
            Assert.Equal("Signature", lastCommandTree.Parameters.ElementAt(3).Key);
            Assert.Equal("Name_Original", lastCommandTree.Parameters.ElementAt(4).Key);
            Assert.Equal("RowVersion_Original", lastCommandTree.Parameters.ElementAt(5).Key);
            Assert.Equal("Customer_CustomerId", lastCommandTree.Parameters.ElementAt(6).Key);
        }