Example #1
0
        public void Can_detect_removed_modification_functions()
        {
            var modelBuilder = new DbModelBuilder();

            var model1 = modelBuilder.Build(ProviderInfo);

            modelBuilder.Entity<OrderLine>().MapToStoredProcedures();

            var model2 = new TestContext();

            var dropProcedureOperations
                = new EdmModelDiffer().Diff(model2.GetModel(), model1.GetModel())
                    .OfType<DropProcedureOperation>()
                    .ToList();

            Assert.Equal(20, dropProcedureOperations.Count);
            Assert.True(dropProcedureOperations.All(c => c.Name.Any()));
        }
Example #2
0
            GetAssociationModificationFunctionMapping(string associationName)
        {
            MetadataWorkspace metadataWorkspace;

            using (var context = new TestContext())
            {
                metadataWorkspace = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace;
            }

            var entityContainer
                = metadataWorkspace
                    .GetItems<EntityContainer>(DataSpace.CSpace)
                    .Single();

            var associationType
                = metadataWorkspace
                    .GetItem<AssociationType>(typeof(TestContext).Namespace + "." + associationName, DataSpace.CSpace);

            var storageEntityContainerMapping
                = (StorageEntityContainerMapping)metadataWorkspace.GetMap(entityContainer, DataSpace.CSSpace);

            var modificationFunctionMapping
                = storageEntityContainerMapping
                    .AssociationSetMappings
                    .Select(esm => esm.ModificationFunctionMapping)
                    .Single(
                        mfm => mfm != null
                               && mfm.AssociationSet.ElementType == associationType);

            return Tuple.Create(modificationFunctionMapping, storageEntityContainerMapping);
        }
Example #3
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(),
                        new Lazy<ModificationCommandTreeGenerator>(() => commandTreeGenerator),
                        new SqlServerMigrationSqlGenerator())
                    .OfType<CreateProcedureOperation>()
                    .ToList();

            Assert.Equal(20, createProcedureOperations.Count);
            Assert.True(createProcedureOperations.All(c => c.Name.Any()));
            Assert.True(createProcedureOperations.All(c => c.BodySql.Any()));
        }
Example #4
0
 public static DbModel CreateDynamicUpdateModel()
 {
     using (var context = new TestContext())
     {
         return context
             .InternalContext
             .CodeFirstModel
             .CachedModelBuilder
             .BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
     }
 }
Example #5
0
            GetModificationFunctionMapping(string entityName)
        {
            MetadataWorkspace metadataWorkspace;

            using (var context = new TestContext())
            {
                metadataWorkspace = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace;
            }

            var entityContainer
                = metadataWorkspace
                    .GetItems<EntityContainer>(DataSpace.CSpace)
                    .Single();

            var entityType
                = metadataWorkspace
                    .GetItem<EntityType>(typeof(TestContext).Namespace + "." + entityName, DataSpace.CSpace);

            var storageEntityContainerMapping
                = (StorageEntityContainerMapping)metadataWorkspace.GetMap(entityContainer, DataSpace.CSSpace);

            var modificationFunctionMapping
                = storageEntityContainerMapping
                    .EntitySetMappings
                    .SelectMany(esm => esm.ModificationFunctionMappings)
                    .Single(mfm => mfm.EntityType == entityType);

            return Tuple.Create(modificationFunctionMapping, storageEntityContainerMapping);
        }
        public void Generate_can_output_rename_procedure_statements()
        {
            var model1 = new TestContext();
            var model2 = new TestContext_v2();

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

            var renameProcedureOperation
                = new EdmModelDiffer()
                    .Diff(
                        model1.GetModel(),
                        model2.GetModel(),
                        new Lazy<ModificationCommandTreeGenerator>(() => commandTreeGenerator),
                        new SqlServerMigrationSqlGenerator())
                    .OfType<RenameProcedureOperation>()
                    .Single();

            var migrationSqlGenerator = new SqlServerMigrationSqlGenerator();

            var sql = migrationSqlGenerator.Generate(new[] { renameProcedureOperation }, "2008").Join(s => s.Sql, Environment.NewLine);

            Assert.Equal(@"EXECUTE sp_rename @objname = N'dbo.Order_Insert', @newname = N'sproc_A', @objtype = N'OBJECT'", sql);
        }
        public void Generate_can_output_alter_procedure_statements()
        {
            var model1 = new TestContext();
            var model2 = new TestContext_v2();

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

            var alterProcedureOperation
                = new EdmModelDiffer()
                    .Diff(
                        model1.GetModel(),
                        model2.GetModel(),
                        new Lazy<ModificationCommandTreeGenerator>(() => commandTreeGenerator),
                        new SqlServerMigrationSqlGenerator())
                    .OfType<AlterProcedureOperation>()
                    .Single(c => c.Name == "dbo.Order_Update");

            var migrationSqlGenerator = new SqlServerMigrationSqlGenerator();

            var sql = migrationSqlGenerator.Generate(new[] { alterProcedureOperation }, "2008").Join(s => s.Sql, Environment.NewLine);

            Assert.Equal(
                @"ALTER PROCEDURE [dbo].[Order_Update]
    @order_id [int],
    @key_for_update2 [uniqueidentifier],
    @Code [nvarchar](128),
    @Signature [varbinary](128),
    @Name [nvarchar](max),
    @Name_Original [nvarchar](max),
    @Address_Street [nvarchar](max),
    @Address_City [nvarchar](max),
    @Address_CountryOrRegion_Name [nvarchar](max),
    @OrderGroupId [int],
    @RowVersion_Original [rowversion],
    @Customer_CustomerId [int]
AS
BEGIN
    UPDATE [dbo].[Orders]
    SET [Name] = @Name, [Address_Street] = @Address_Street, [Address_City] = @Address_City, [Address_CountryOrRegion_Name] = @Address_CountryOrRegion_Name, [OrderGroupId] = @OrderGroupId, [Customer_CustomerId] = @Customer_CustomerId
    WHERE (((((([order_id] = @order_id) AND ([Key] = @key_for_update2)) AND ([Code] = @Code)) AND ([Signature] = @Signature)) AND (([Name] = @Name_Original) OR ([Name] IS NULL AND @Name_Original IS NULL))) AND (([RowVersion] = @RowVersion_Original) OR ([RowVersion] IS NULL AND @RowVersion_Original IS NULL)))
    
    SELECT t0.[OrderNo], t0.[RowVersion]
    FROM [dbo].[Orders] AS t0
    WHERE @@ROWCOUNT > 0 AND t0.[order_id] = @order_id AND t0.[Key] = @key_for_update2 AND t0.[Code] = @Code AND t0.[Signature] = @Signature
END", sql);
        }
        public void Generate_can_output_create_procedure_statements()
        {
            var modelBuilder = new DbModelBuilder();

            var model1 = modelBuilder.Build(ProviderRegistry.Sql2008_ProviderInfo);

            var model2 = new TestContext();

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

            var createProcedureOperation
                = new EdmModelDiffer()
                    .Diff(
                        model1.GetModel(),
                        model2.GetModel(),
                        new Lazy<ModificationCommandTreeGenerator>(() => commandTreeGenerator),
                        new SqlServerMigrationSqlGenerator())
                    .OfType<CreateProcedureOperation>()
                    .Single(c => c.Name == "dbo.ExtraSpecialOrder_Update");

            var migrationSqlGenerator = new SqlServerMigrationSqlGenerator();

            var sql = migrationSqlGenerator.Generate(new[] { createProcedureOperation }, "2008").Join(s => s.Sql, Environment.NewLine);

            Assert.Equal(
                @"CREATE PROCEDURE [dbo].[ExtraSpecialOrder_Update]
    @xid [int],
    @key_for_update [uniqueidentifier],
    @Code [nvarchar](128),
    @Signature [varbinary](128),
    @Name [nvarchar](max),
    @Name_Original [nvarchar](max),
    @Address_Street [nvarchar](max),
    @Address_City [nvarchar](max),
    @Address_CountryOrRegion_Name [nvarchar](max),
    @OrderGroupId [int],
    @RowVersion_Original [rowversion],
    @OtherAddress_Street [nvarchar](max),
    @OtherAddress_City [nvarchar](max),
    @OtherAddress_CountryOrRegion_Name [nvarchar](max),
    @TheSpecialist [int],
    @Customer_CustomerId [int],
    @OtherCustomer_CustomerId [int]
AS
BEGIN
    UPDATE [dbo].[Orders]
    SET [Name] = @Name, [Address_Street] = @Address_Street, [Address_City] = @Address_City, [Address_CountryOrRegion_Name] = @Address_CountryOrRegion_Name, [OrderGroupId] = @OrderGroupId, [Customer_CustomerId] = @Customer_CustomerId
    WHERE (((((([order_id] = @xid) AND ([Key] = @key_for_update)) AND ([Code] = @Code)) AND ([Signature] = @Signature)) AND (([Name] = @Name_Original) OR ([Name] IS NULL AND @Name_Original IS NULL))) AND (([RowVersion] = @RowVersion_Original) OR ([RowVersion] IS NULL AND @RowVersion_Original IS NULL)))
    
    UPDATE [dbo].[special_orders]
    SET [OtherCustomer_CustomerId] = @OtherCustomer_CustomerId, [OtherAddress_Street] = @OtherAddress_Street, [OtherAddress_City] = @OtherAddress_City, [OtherAddress_CountryOrRegion_Name] = @OtherAddress_CountryOrRegion_Name
    WHERE (((([order_id] = @xid) AND ([so_key] = @key_for_update)) AND ([Code] = @Code)) AND ([Signature] = @Signature))
    AND @@ROWCOUNT > 0
    
    UPDATE [dbo].[xspecial_orders]
    SET [TheSpecialist] = @TheSpecialist
    WHERE (((([xid] = @xid) AND ([so_key] = @key_for_update)) AND ([Code] = @Code)) AND ([Signature] = @Signature))
    AND @@ROWCOUNT > 0
    
    SELECT t0.[OrderNo] AS order_fu, t0.[RowVersion], t1.[MagicOrderToken], t2.[FairyDust]
    FROM [dbo].[Orders] AS t0
    JOIN [dbo].[special_orders] AS t1 ON t1.[order_id] = t0.[order_id] AND t1.[so_key] = t0.[Key] AND t1.[Code] = t0.[Code] AND t1.[Signature] = t0.[Signature]
    JOIN [dbo].[xspecial_orders] AS t2 ON t2.[xid] = t0.[order_id] AND t2.[so_key] = t0.[Key] AND t2.[Code] = t0.[Code] AND t2.[Signature] = t0.[Signature]
    WHERE @@ROWCOUNT > 0 AND t0.[order_id] = @xid AND t0.[Key] = @key_for_update AND t0.[Code] = @Code AND t0.[Signature] = @Signature
END", sql);
        }
 public static DbModel CreateDynamicUpdateModel()
 {
     using (var context = new TestContext())
     {
         return context.GetDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo);
     }
 }
Example #10
0
 public static DbModel CreateDynamicUpdateModel()
 {
     using (var context = new TestContext())
     {
         var providerInfo = DatabaseTestHelpers.IsSqlAzure(context.Database.Connection.ConnectionString)
                                ? ProviderRegistry.SqlAzure2012_ProviderInfo
                                : ProviderRegistry.Sql2008_ProviderInfo;
                                            
         return context
             .InternalContext
             .CodeFirstModel
             .CachedModelBuilder
             .BuildDynamicUpdateModel(providerInfo);
     }
 }