public void Update_simple_entity() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Customer"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType <DbUpdateCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"UPDATE [dbo].[Customers] SET [Name] = @Name WHERE ([CustomerId] = @CustomerId)", functionSqlGenerator.GenerateUpdate(convertedTrees, null)); }
public void Insert_simple_tph_entity_derived() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Car"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateInsert(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType<DbInsertCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"INSERT [dbo].[Vehicles]([Name], [Discriminator]) VALUES (@Name, N'Car') DECLARE @Id int SELECT @Id = [Id] FROM [dbo].[Vehicles] WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity() SELECT t0.[Id] FROM [dbo].[Vehicles] AS t0 WHERE @@ROWCOUNT > 0 AND t0.[Id] = @Id", functionSqlGenerator.GenerateInsert(convertedTrees)); }
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); }
public void Delete_tpt_entity_with_concurrency_and_store_generated_values() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateDelete(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType <DbDeleteCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"DELETE [dbo].[xspecial_orders] WHERE (((([xid] = @xid) AND ([so_key] = @key_for_delete)) AND ([Code] = @Code)) AND ([Signature] = @Signature)) DELETE [dbo].[special_orders] WHERE ((((([order_id] = @xid) AND ([so_key] = @key_for_delete)) AND ([Code] = @Code)) AND ([Signature] = @Signature)) AND (([OtherCustomer_CustomerId] = @OtherCustomer_CustomerId) OR ([OtherCustomer_CustomerId] IS NULL AND @OtherCustomer_CustomerId IS NULL))) AND @@ROWCOUNT > 0 DELETE [dbo].[Orders] WHERE ((((((([order_id] = @xid) AND ([Key] = @key_for_delete)) 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))) AND (([Customer_CustomerId] = @Customer_CustomerId) OR ([Customer_CustomerId] IS NULL AND @Customer_CustomerId IS NULL))) AND @@ROWCOUNT > 0 SET @rows_affected = @@ROWCOUNT", functionSqlGenerator.GenerateDelete(convertedTrees, "rows_affected")); }
public void Update_value_type_concurrency_token() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Person"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType <DbUpdateCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"UPDATE [dbo].[People] SET [BirthDate] = @BirthDate WHERE (([Id] = @Id) AND ([BirthDate] = @BirthDate_Original))", functionSqlGenerator.GenerateUpdate(convertedTrees, null)); }
public void Insert_simple_entity() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Customer"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateInsert(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal( @"insert [dbo].[Customers]([Name]) values (@Name) declare @CustomerId int select @CustomerId = [CustomerId] from [dbo].[Customers] where @@ROWCOUNT > 0 and [CustomerId] = scope_identity() select t0.[CustomerId] from [dbo].[Customers] as t0 where @@ROWCOUNT > 0 and t0.[CustomerId] = @CustomerId", functionSqlGenerator.GenerateInsert(convertedTrees)); }
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()); }
public void Insert_simple_tph_entity_derived() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Car"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateInsert(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType <DbInsertCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"INSERT [dbo].[Vehicles]([Name], [Discriminator]) VALUES (@Name, N'Car') DECLARE @Id int SELECT @Id = [Id] FROM [dbo].[Vehicles] WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity() SELECT t0.[Id] FROM [dbo].[Vehicles] AS t0 WHERE @@ROWCOUNT > 0 AND t0.[Id] = @Id", functionSqlGenerator.GenerateInsert(convertedTrees)); }
public void Delete_tpt_entity_with_concurrency_and_store_generated_values() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateDelete(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal( @"delete [dbo].[xspecial_orders] where (((([xid] = @xid) and ([so_key] = @key_for_delete)) and ([Code] = @Code)) and ([Signature] = @Signature)) delete [dbo].[special_orders] where ((((([order_id] = @xid) and ([so_key] = @key_for_delete)) and ([Code] = @Code)) and ([Signature] = @Signature)) and (([OtherCustomer_CustomerId] = @OtherCustomer_CustomerId) or ([OtherCustomer_CustomerId] is null and @OtherCustomer_CustomerId is null))) and @@ROWCOUNT > 0 delete [dbo].[Orders] where ((((((([order_id] = @xid) and ([Key] = @key_for_delete)) 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))) and (([Customer_CustomerId] = @Customer_CustomerId) or ([Customer_CustomerId] is null and @Customer_CustomerId is null))) and @@ROWCOUNT > 0 set @rows_affected = @@ROWCOUNT", functionSqlGenerator.GenerateDelete(convertedTrees, "rows_affected")); }
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_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 Insert_tpt_entity_with_concurrency_and_store_generated_values() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateInsert(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType <DbInsertCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"DECLARE @generated_keys table([order_id] int, [Key] uniqueidentifier, [Code] nvarchar(128), [Signature] varbinary(128)) INSERT [dbo].[Orders]([Code], [Signature], [Name], [Address_Street], [Address_City], [Address_Country_Name], [OrderGroupId], [Customer_CustomerId]) OUTPUT inserted.[order_id], inserted.[Key], inserted.[Code], inserted.[Signature] INTO @generated_keys VALUES (@teh_codez, @Signature, @the_name, @Address_Street, @Address_City, @Address_Country_Name, @OrderGroupId, @Customer_CustomerId) DECLARE @order_id int, @Key uniqueidentifier SELECT @order_id = t.[order_id], @Key = t.[Key] FROM @generated_keys AS g JOIN [dbo].[Orders] AS t ON g.[order_id] = t.[order_id] AND g.[Key] = t.[Key] AND g.[Code] = t.[Code] AND g.[Signature] = t.[Signature] WHERE @@ROWCOUNT > 0 INSERT [dbo].[special_orders]([order_id], [so_key], [Code], [Signature], [OtherCustomer_CustomerId], [OtherAddress_Street], [OtherAddress_City], [OtherAddress_Country_Name]) VALUES (@order_id, @Key, @teh_codez, @Signature, @OtherCustomer_CustomerId, @OtherAddress_Street, @OtherAddress_City, @OtherAddress_Country_Name) INSERT [dbo].[xspecial_orders]([xid], [so_key], [Code], [Signature], [TheSpecialist]) VALUES (@order_id, @Key, @teh_codez, @Signature, @TheSpecialist) SELECT t0.[order_id] AS xid, t0.[Key] AS key_result, t0.[OrderNo], 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] = @order_id AND t0.[Key] = @Key AND t0.[Code] = @teh_codez AND t0.[Signature] = @Signature", functionSqlGenerator.GenerateInsert(convertedTrees)); }
public void Update_tpt_entity_with_concurrency_and_store_generated_values() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType <DbUpdateCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"UPDATE [dbo].[Orders] SET [Name] = @Name, [Address_Street] = @Address_Street, [Address_City] = @Address_City, [Address_Country_Name] = @Address_Country_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_Country_Name] = @OtherAddress_Country_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 SET @rows_affected = @@ROWCOUNT", functionSqlGenerator.GenerateUpdate(convertedTrees, "rows_affected")); }
public void Insert_tpt_entity_with_concurrency_and_store_generated_values() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateInsert(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal( @"declare @generated_keys table([order_id] int, [Key] uniqueidentifier, [Code] nvarchar(128), [Signature] varbinary(128)) insert [dbo].[Orders]([Code], [Signature], [Name], [Address_Street], [Address_City], [Address_Country_Name], [OrderGroupId], [Customer_CustomerId]) output inserted.[order_id], inserted.[Key], inserted.[Code], inserted.[Signature] into @generated_keys values (@teh_codez, @Signature, @the_name, @Address_Street, @Address_City, @Address_Country_Name, @OrderGroupId, @Customer_CustomerId) declare @order_id int, @Key uniqueidentifier select @order_id = t.[order_id], @Key = t.[Key] from @generated_keys as g join [dbo].[Orders] as t on g.[order_id] = t.[order_id] and g.[Key] = t.[Key] and g.[Code] = t.[Code] and g.[Signature] = t.[Signature] where @@ROWCOUNT > 0 insert [dbo].[special_orders]([order_id], [so_key], [Code], [Signature], [OtherCustomer_CustomerId], [OtherAddress_Street], [OtherAddress_City], [OtherAddress_Country_Name]) values (@order_id, @Key, @teh_codez, @Signature, @OtherCustomer_CustomerId, @OtherAddress_Street, @OtherAddress_City, @OtherAddress_Country_Name) insert [dbo].[xspecial_orders]([xid], [so_key], [Code], [Signature], [TheSpecialist]) values (@order_id, @Key, @teh_codez, @Signature, @TheSpecialist) select t0.[order_id] as xid, t0.[Key] as key_result, t0.[OrderNo], 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] = @order_id and t0.[Key] = @Key and t0.[Code] = @teh_codez and t0.[Signature] = @Signature", functionSqlGenerator.GenerateInsert(convertedTrees)); }
public void Update_tpt_entity_with_concurrency_and_store_generated_values() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal( @"update [dbo].[Orders] set [Name] = @Name, [Address_Street] = @Address_Street, [Address_City] = @Address_City, [Address_Country_Name] = @Address_Country_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))) and (([Customer_CustomerId] = @Customer_CustomerId) or ([Customer_CustomerId] is null and @Customer_CustomerId is null))) update [dbo].[special_orders] set [OtherCustomer_CustomerId] = @OtherCustomer_CustomerId, [OtherAddress_Street] = @OtherAddress_Street, [OtherAddress_City] = @OtherAddress_City, [OtherAddress_Country_Name] = @OtherAddress_Country_Name where ((((([order_id] = @xid) and ([so_key] = @key_for_update)) and ([Code] = @Code)) and ([Signature] = @Signature)) and (([OtherCustomer_CustomerId] = @OtherCustomer_CustomerId) or ([OtherCustomer_CustomerId] is null and @OtherCustomer_CustomerId is null))) 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 set @rows_affected = @@ROWCOUNT", functionSqlGenerator.GenerateUpdate(convertedTrees, "rows_affected")); }
public IEnumerable<MigrationOperation> Diff( XDocument sourceModel, XDocument targetModel, ModificationCommandTreeGenerator modificationCommandTreeGenerator = null, MigrationSqlGenerator migrationSqlGenerator = null) { DebugCheck.NotNull(sourceModel); DebugCheck.NotNull(targetModel); DbProviderInfo providerInfo; var storageMappingItemCollection = sourceModel.GetStorageMappingItemCollection(out providerInfo); var source = new ModelMetadata { Model = sourceModel, StoreItemCollection = storageMappingItemCollection.StoreItemCollection, StorageEntityContainerMapping = storageMappingItemCollection.GetItems<StorageEntityContainerMapping>().Single(), ProviderManifest = GetProviderManifest(providerInfo), ProviderInfo = providerInfo }; storageMappingItemCollection = targetModel.GetStorageMappingItemCollection(out providerInfo); var target = new ModelMetadata { Model = targetModel, StoreItemCollection = storageMappingItemCollection.StoreItemCollection, StorageEntityContainerMapping = storageMappingItemCollection.GetItems<StorageEntityContainerMapping>().Single(), ProviderManifest = GetProviderManifest(providerInfo), ProviderInfo = providerInfo }; return Diff(source, target, modificationCommandTreeGenerator, migrationSqlGenerator); }
public void Update_simple_entity_with_tph_base() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Vehicle"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType <DbUpdateCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Null(functionSqlGenerator.GenerateUpdate(convertedTrees, null)); }
public void Can_generate_dynamic_insert_command_trees() { var model = TestContext.CreateDynamicUpdateModel(); var commandTreeGenerator = new ModificationCommandTreeGenerator(model); var commandTrees = commandTreeGenerator .GenerateInsert(GetType().Namespace + ".FunctionsModel.SpecialOrder") .ToList(); Assert.Equal(2, commandTrees.Count()); var commandTree = commandTrees.First(); Assert.Equal(8, commandTree.SetClauses.Count); Assert.Equal("Order", commandTree.Target.VariableType.EdmType.Name); Assert.NotNull(commandTree.Returning); commandTree = commandTrees.Last(); Assert.Equal(8, commandTree.SetClauses.Count); Assert.NotNull(commandTree.Returning); Assert.Equal("special_orders", commandTree.Target.VariableType.EdmType.Name); commandTrees = commandTreeGenerator .GenerateInsert(GetType().Namespace + ".FunctionsModel.Customer") .ToList(); Assert.Equal(1, commandTrees.Count()); commandTree = commandTrees.Single(); Assert.Equal(1, commandTree.SetClauses.Count); Assert.Equal("Customer", commandTree.Target.VariableType.EdmType.Name); Assert.NotNull(commandTree.Returning); }
public void Delete_simple_entity() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Customer"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateDelete(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal( @"delete [dbo].[Customers] where ([CustomerId] = @CustomerId)", functionSqlGenerator.GenerateDelete(convertedTrees, null)); }
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 Delete_simple_entity_with_tph_derived() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Car"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateDelete(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType <DbDeleteCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"DELETE [dbo].[Vehicles] WHERE ([Id] = @Id)", functionSqlGenerator.GenerateDelete(convertedTrees, null)); }
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 void Can_detect_changed_modification_functions_when_column_change_affects_parameter() { var commandTreeGenerator = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()); var targetModel = new TestContext_v2c().GetModel(); var alterProcedureOperations = new EdmModelDiffer() .Diff( new TestContext().GetModel(), targetModel, new Lazy<ModificationCommandTreeGenerator>(() => commandTreeGenerator), new SqlServerMigrationSqlGenerator()) .OfType<AlterProcedureOperation>() .ToList(); Assert.Equal(2, alterProcedureOperations.Count); Assert.True(alterProcedureOperations.All(c => c.BodySql.Any())); Assert.True(alterProcedureOperations .SelectMany(a => a.Parameters).Any(p => p.Name == "Name" && p.MaxLength == 42)); }
public void Update_simple_entity() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Customer"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(ProviderRegistry.Sql2008_ProviderManifest); Assert.Equal( @"update [dbo].[Customers] set [Name] = @Name where ([CustomerId] = @CustomerId)", functionSqlGenerator.GenerateUpdate(convertedTrees, null)); }
internal DbMigrator(DbMigrationsConfiguration configuration, DbContext usersContext) : base(null) { Check.NotNull(configuration, "configuration"); Check.NotNull(configuration.ContextType, "configuration.ContextType"); _configuration = configuration; _calledByCreateDatabase = usersContext != null; // If DbContext CreateDatabase is using Migrations then the user has not opted out of initializers // and if we disable the initializer here then future calls to Initialize the database (for this or // a different connection) will fail. So only disable the initializer if Migrations are being used // explicitly. if (usersContext == null) { DisableInitializer(_configuration.ContextType); } if (_calledByCreateDatabase) { _usersContextInfo = new DbContextInfo(usersContext); } else { _usersContextInfo = configuration.TargetDatabase == null ? new DbContextInfo(configuration.ContextType) : new DbContextInfo(configuration.ContextType, configuration.TargetDatabase); if (!_usersContextInfo.IsConstructible) { throw Error.ContextNotConstructible(configuration.ContextType); } } _modelDiffer = _configuration.ModelDiffer; var context = usersContext ?? _usersContextInfo.CreateInstance(); _contextForInterception = context; try { _migrationAssembly = new MigrationAssembly( _configuration.MigrationsAssembly, _configuration.MigrationsNamespace); _currentModel = context.GetModel(); var connection = context.Database.Connection; _providerFactory = DbProviderServices.GetProviderFactory(connection); _defaultSchema = context.InternalContext.DefaultSchema ?? EdmModelExtensions.DefaultSchema; _historyRepository = new HistoryRepository( _usersContextInfo.ConnectionString, _providerFactory, _configuration.ContextKey, _configuration.CommandTimeout, new[] { _defaultSchema }.Concat(GetHistorySchemas()), _configuration.HistoryContextFactory); _providerManifestToken = context.InternalContext.ModelProviderInfo != null ? context.InternalContext.ModelProviderInfo.ProviderManifestToken : DbConfiguration .GetService<IManifestTokenService>() .GetProviderManifestToken(connection); _modificationCommandTreeGenerator = new ModificationCommandTreeGenerator( context.GetDynamicUpdateModel( new DbProviderInfo( _usersContextInfo.ConnectionProviderName, _providerManifestToken)), CreateConnection()); _targetDatabase = Strings.LoggingTargetDatabaseFormat( connection.DataSource, connection.Database, _usersContextInfo.ConnectionProviderName, _usersContextInfo.ConnectionStringOrigin == DbConnectionStringOrigin.DbContextInfo ? Strings.LoggingExplicit : _usersContextInfo.ConnectionStringOrigin.ToString()); _legacyContextKey = context.InternalContext.ContextKey; _emptyModel = GetEmptyModel(); } finally { if (usersContext == null) { _contextForInterception = null; context.Dispose(); } } }
public void Can_detect_changed_modification_functions() { var commandTreeGenerator = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()); var targetModel = new TestContext_v2().GetModel(); var alterProcedureOperations = new EdmModelDiffer() .Diff( new TestContext().GetModel(), targetModel, new Lazy<ModificationCommandTreeGenerator>(() => commandTreeGenerator), new SqlServerMigrationSqlGenerator()) .OfType<AlterProcedureOperation>() .ToList(); Assert.Equal(3, alterProcedureOperations.Count); Assert.True(alterProcedureOperations.All(c => c.BodySql.Any())); Assert.Equal(1, alterProcedureOperations.Count(c => c.Parameters.Any(p => p.Name == "key_for_update2"))); Assert.Equal(1, alterProcedureOperations.Count(c => c.Parameters.Any(p => p.Name == "affected_rows"))); }
public void Insert_tpt_entity_with_concurrency_and_store_generated_values() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateInsert(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType<DbInsertCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"DECLARE @generated_keys table([order_id] int, [Key] uniqueidentifier, [Code] nvarchar(128), [Signature] varbinary(128)) INSERT [dbo].[Orders]([Code], [Signature], [Name], [Address_Street], [Address_City], [Address_CountryOrRegion_Name], [OrderGroupId], [Customer_CustomerId]) OUTPUT inserted.[order_id], inserted.[Key], inserted.[Code], inserted.[Signature] INTO @generated_keys VALUES (@teh_codez, @Signature, @the_name, @Address_Street, @Address_City, @Address_CountryOrRegion_Name, @OrderGroupId, @Customer_CustomerId) DECLARE @order_id int, @Key uniqueidentifier SELECT @order_id = t.[order_id], @Key = t.[Key] FROM @generated_keys AS g JOIN [dbo].[Orders] AS t ON g.[order_id] = t.[order_id] AND g.[Key] = t.[Key] AND g.[Code] = t.[Code] AND g.[Signature] = t.[Signature] WHERE @@ROWCOUNT > 0 INSERT [dbo].[special_orders]([order_id], [so_key], [Code], [Signature], [OtherCustomer_CustomerId], [OtherAddress_Street], [OtherAddress_City], [OtherAddress_CountryOrRegion_Name]) VALUES (@order_id, @Key, @teh_codez, @Signature, @OtherCustomer_CustomerId, @OtherAddress_Street, @OtherAddress_City, @OtherAddress_CountryOrRegion_Name) INSERT [dbo].[xspecial_orders]([xid], [so_key], [Code], [Signature], [TheSpecialist]) VALUES (@order_id, @Key, @teh_codez, @Signature, @TheSpecialist) SELECT t0.[order_id] AS xid, t0.[Key] AS key_result, t0.[OrderNo], 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] = @order_id AND t0.[Key] = @Key AND t0.[Code] = @teh_codez AND t0.[Signature] = @Signature", functionSqlGenerator.GenerateInsert(convertedTrees)); }
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(), commandTreeGenerator, new SqlServerMigrationSqlGenerator()) .OfType <CreateProcedureOperation>() .Single(c => c.Name == "ExtraSpecialOrder_Update"); var migrationSqlGenerator = new SqlServerMigrationSqlGenerator(); var sql = migrationSqlGenerator.Generate(new[] { createProcedureOperation }, "2008").Join(s => s.Sql, Environment.NewLine); Assert.Equal( @"CREATE PROCEDURE [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_Country_Name [nvarchar](max), @OrderGroupId [int], @RowVersion_Original [rowversion], @OtherAddress_Street [nvarchar](max), @OtherAddress_City [nvarchar](max), @OtherAddress_Country_Name [nvarchar](max), @TheSpecialist [int], @Customer_CustomerId [int], @OtherCustomer_CustomerId [int], @RowsAffected [int] OUT AS BEGIN UPDATE [dbo].[Orders] SET [Name] = @Name, [Address_Street] = @Address_Street, [Address_City] = @Address_City, [Address_Country_Name] = @Address_Country_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))) and (([Customer_CustomerId] = @Customer_CustomerId) or ([Customer_CustomerId] is null and @Customer_CustomerId is null))) UPDATE [dbo].[special_orders] SET [OtherCustomer_CustomerId] = @OtherCustomer_CustomerId, [OtherAddress_Street] = @OtherAddress_Street, [OtherAddress_City] = @OtherAddress_City, [OtherAddress_Country_Name] = @OtherAddress_Country_Name WHERE ((((([order_id] = @xid) and ([so_key] = @key_for_update)) and ([Code] = @Code)) and ([Signature] = @Signature)) and (([OtherCustomer_CustomerId] = @OtherCustomer_CustomerId) or ([OtherCustomer_CustomerId] is null and @OtherCustomer_CustomerId is null))) 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 SET @RowsAffected = @@ROWCOUNT END", sql); }
public void Delete_simple_entity() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Customer"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateDelete(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType<DbDeleteCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"DELETE [dbo].[Customers] WHERE ([CustomerId] = @CustomerId)", functionSqlGenerator.GenerateDelete(convertedTrees, null)); }
public void Update_tpt_entity_with_concurrency_and_store_generated_values() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType<DbUpdateCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"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 SET @rows_affected = @@ROWCOUNT", functionSqlGenerator.GenerateUpdate(convertedTrees, "rows_affected")); }
public void Delete_tpt_entity_with_concurrency_and_store_generated_values() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("ExtraSpecialOrder"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateDelete(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType<DbDeleteCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"DELETE [dbo].[xspecial_orders] WHERE (((([xid] = @xid) AND ([so_key] = @key_for_delete)) AND ([Code] = @Code)) AND ([Signature] = @Signature)) DELETE [dbo].[special_orders] WHERE ((((([order_id] = @xid) AND ([so_key] = @key_for_delete)) AND ([Code] = @Code)) AND ([Signature] = @Signature)) AND (([OtherCustomer_CustomerId] = @OtherCustomer_CustomerId) OR ([OtherCustomer_CustomerId] IS NULL AND @OtherCustomer_CustomerId IS NULL))) AND @@ROWCOUNT > 0 DELETE [dbo].[Orders] WHERE ((((((([order_id] = @xid) AND ([Key] = @key_for_delete)) 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))) AND (([Customer_CustomerId] = @Customer_CustomerId) OR ([Customer_CustomerId] IS NULL AND @Customer_CustomerId IS NULL))) AND @@ROWCOUNT > 0 SET @rows_affected = @@ROWCOUNT", functionSqlGenerator.GenerateDelete(convertedTrees, "rows_affected")); }
public void Delete_value_type_concurrency_token() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Person"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateDelete(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType<DbDeleteCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"DELETE [dbo].[People] WHERE (([Id] = @Id) AND ([BirthDate] = @BirthDate_Original))", functionSqlGenerator.GenerateDelete(convertedTrees, null)); }
public void Update_exceptions_should_be_wrapped_when_generating_sproc_bodies() { var modelBuilder = new DbModelBuilder(); var model1 = modelBuilder.Build(ProviderInfo); var context = new WorldContext_Invalid(); var commandTreeGenerator = new ModificationCommandTreeGenerator( context .InternalContext .CodeFirstModel .CachedModelBuilder .BuildDynamicUpdateModel(ProviderRegistry.Sql2008_ProviderInfo)); Assert.Throws<InvalidOperationException>( () => new EdmModelDiffer() .Diff( model1.GetModel(), context.GetModel(), new Lazy<ModificationCommandTreeGenerator>(() => commandTreeGenerator), new SqlServerMigrationSqlGenerator()) .OfType<CreateProcedureOperation>() .ToList()) .ValidateMessage("ErrorGeneratingCommandTree", "Thing_Insert", "Thing"); }
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 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); }
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 Update_simple_entity_with_tph_derived() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Car"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType<DbUpdateCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Equal( @"UPDATE [dbo].[Vehicles] SET [Name] = @Name WHERE ([Id] = @Id)", functionSqlGenerator.GenerateUpdate(convertedTrees, null)); }
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())); }
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 = 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_Country_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_detect_changed_modification_functions_when_many_to_many() { var commandTreeGenerator = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()); var targetModel = new TestContext_v2b().GetModel(); var alterProcedureOperations = new EdmModelDiffer() .Diff( new TestContext().GetModel(), targetModel, new Lazy<ModificationCommandTreeGenerator>(() => commandTreeGenerator), new SqlServerMigrationSqlGenerator()) .OfType<AlterProcedureOperation>() .ToList(); Assert.Equal(2, alterProcedureOperations.Count); Assert.Equal(1, alterProcedureOperations.Count(ap => ap.Parameters.Any(p => p.Name == "order_thing_id"))); Assert.Equal(1, alterProcedureOperations.Count(ap => ap.Parameters.Any(p => p.Name == "order_id"))); }
internal DbMigrator(DbMigrationsConfiguration configuration, DbContext usersContext) : base(null) { Check.NotNull(configuration, "configuration"); Check.NotNull(configuration.ContextType, "configuration.ContextType"); _configuration = configuration; _calledByCreateDatabase = usersContext != null; // If DbContext CreateDatabase is using Migrations then the user has not opted out of initializers // and if we disable the initializer here then future calls to Initialize the database (for this or // a different connection) will fail. So only disable the initializer if Migrations are being used // explicitly. if (usersContext == null) { DisableInitializer(_configuration.ContextType); } if (_calledByCreateDatabase) { _usersContextInfo = new DbContextInfo(usersContext); } else { _usersContextInfo = configuration.TargetDatabase == null ? new DbContextInfo(configuration.ContextType) : new DbContextInfo(configuration.ContextType, configuration.TargetDatabase); if (!_usersContextInfo.IsConstructible) { throw Error.ContextNotConstructible(configuration.ContextType); } } _modelDiffer = _configuration.ModelDiffer; var context = usersContext ?? _usersContextInfo.CreateInstance(); _contextForInterception = context; try { _migrationAssembly = new MigrationAssembly( _configuration.MigrationsAssembly, _configuration.MigrationsNamespace); _currentModel = context.GetModel(); var connection = context.Database.Connection; _providerFactory = DbProviderServices.GetProviderFactory(connection); _defaultSchema = context.InternalContext.DefaultSchema ?? EdmModelExtensions.DefaultSchema; _historyRepository = new HistoryRepository( _usersContextInfo.ConnectionString, _providerFactory, _configuration.ContextKey, _configuration.CommandTimeout, new[] { _defaultSchema }.Concat(GetHistorySchemas()), _configuration.HistoryContextFactory); _providerManifestToken = context.InternalContext.ModelProviderInfo != null ? context.InternalContext.ModelProviderInfo.ProviderManifestToken : DbConfiguration .GetService <IManifestTokenService>() .GetProviderManifestToken(connection); _modificationCommandTreeGenerator = new ModificationCommandTreeGenerator( context.GetDynamicUpdateModel( new DbProviderInfo( _usersContextInfo.ConnectionProviderName, _providerManifestToken)), CreateConnection()); _targetDatabase = Strings.LoggingTargetDatabaseFormat( connection.DataSource, connection.Database, _usersContextInfo.ConnectionProviderName, _usersContextInfo.ConnectionStringOrigin == DbConnectionStringOrigin.DbContextInfo ? Strings.LoggingExplicit : _usersContextInfo.ConnectionStringOrigin.ToString()); _legacyContextKey = context.InternalContext.ContextKey; _emptyModel = GetEmptyModel(); } finally { if (usersContext == null) { _contextForInterception = null; context.Dispose(); } } }
public void Can_detect_renamed_modification_functions_when_many_to_many() { var commandTreeGenerator = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()); var targetModel = new TestContext_v2b().GetModel(); var renameProcedureOperations = new EdmModelDiffer() .Diff( new TestContext().GetModel(), targetModel, new Lazy<ModificationCommandTreeGenerator>(() => commandTreeGenerator), new SqlServerMigrationSqlGenerator()) .OfType<RenameProcedureOperation>() .ToList(); Assert.Equal(1, renameProcedureOperations.Count); Assert.Equal(1, renameProcedureOperations.Count(c => c.NewName == "m2m_insert")); }
public void Update_simple_entity_with_tph_base() { var modificationFunctionMapping = TestContext.GetModificationFunctionMapping("Vehicle"); var commandTrees = new ModificationCommandTreeGenerator(TestContext.CreateDynamicUpdateModel()) .GenerateUpdate(modificationFunctionMapping.Item1.EntityType.FullName); var convertedTrees = new DynamicToFunctionModificationCommandConverter( modificationFunctionMapping.Item1, modificationFunctionMapping.Item2) .Convert(commandTrees) .OfType<DbUpdateCommandTree>() .ToList(); var functionSqlGenerator = new DmlFunctionSqlGenerator(new SqlGenerator()); Assert.Null(functionSqlGenerator.GenerateUpdate(convertedTrees, null)); }