/// <summary> /// Gets an operation to drop the associated index on the foreign key column(s). /// </summary> /// <returns> An operation to drop the index. </returns> public virtual DropIndexOperation CreateDropIndexOperation() { var dropIndexOperation = new DropIndexOperation(_inverse.CreateCreateIndexOperation()) { Table = DependentTable }; DependentColumns.Each(c => dropIndexOperation.Columns.Add(c)); return(dropIndexOperation); }
public void CreateCreateIndexOperation_should_return_corresponding_create_index_operation() { var addForeignKeyOperation = new AddForeignKeyOperation { PrincipalTable = "P", DependentTable = "D", Name = "Foo" }; addForeignKeyOperation.DependentColumns.Add("fk"); var createIndexOperation = addForeignKeyOperation.CreateCreateIndexOperation(); Assert.Equal(createIndexOperation.DefaultName, createIndexOperation.Name); Assert.Equal("D", createIndexOperation.Table); Assert.Equal("fk", createIndexOperation.Columns.Single()); }
public void Generate_can_output_create_table_statement() { var createTableOperation = new CreateTableOperation("Customers"); var idColumn = new ColumnModel(PrimitiveTypeKind.Int32) { Name = "Id", IsNullable = true, IsIdentity = true }; createTableOperation.Columns.Add(idColumn); createTableOperation.Columns.Add( new ColumnModel(PrimitiveTypeKind.String) { Name = "Name", IsNullable = false }); createTableOperation.PrimaryKey = new AddPrimaryKeyOperation { Name = "MyPK" }; createTableOperation.PrimaryKey.Columns.Add(idColumn.Name); var codeGenerator = new VisualBasicMigrationCodeGenerator(); var addForeignKeyOperation = new AddForeignKeyOperation { DependentTable = "Customers", PrincipalTable = "Blogs", CascadeDelete = true }; addForeignKeyOperation.DependentColumns.Add("Blog_Id"); addForeignKeyOperation.PrincipalColumns.Add("Id"); var generatedMigration = codeGenerator.Generate( "Migration", new MigrationOperation[] { createTableOperation, addForeignKeyOperation, addForeignKeyOperation.CreateCreateIndexOperation() }, "Source", "Target", "Foo", "Bar"); Assert.Equal( @"Imports System Imports System.Data.Entity.Migrations Namespace Foo Public Partial Class Bar Inherits DbMigration Public Overrides Sub Up() CreateTable( ""Customers"", Function(c) New With { .Id = c.Int(identity := True), .Name = c.String(nullable := False) }) _ .PrimaryKey(Function(t) t.Id, name := ""MyPK"") _ .ForeignKey(""Blogs"", Function(t) t.Blog_Id, cascadeDelete := True) _ .Index(Function(t) t.Blog_Id) End Sub Public Overrides Sub Down() DropIndex(""Customers"", New String() { ""Blog_Id"" }) DropForeignKey(""Customers"", ""Blog_Id"", ""Blogs"") DropTable(""Customers"") End Sub End Class End Namespace ", generatedMigration.UserCode); Assert.Equal( @"' <auto-generated /> Imports System.Data.Entity.Migrations Imports System.Data.Entity.Migrations.Infrastructure Imports System.Resources Namespace Foo Public NotInheritable Partial Class Bar Implements IMigrationMetadata Private ReadOnly Resources As New ResourceManager(GetType(Bar)) Private ReadOnly Property IMigrationMetadata_Id() As String Implements IMigrationMetadata.Id Get Return ""Migration"" End Get End Property Private ReadOnly Property IMigrationMetadata_Source() As String Implements IMigrationMetadata.Source Get Return Resources.GetString(""Source"") End Get End Property Private ReadOnly Property IMigrationMetadata_Target() As String Implements IMigrationMetadata.Target Get Return Resources.GetString(""Target"") End Get End Property End Class End Namespace ", generatedMigration.DesignerCode); Assert.Equal("vb", generatedMigration.Language); Assert.Equal(2, generatedMigration.Resources.Count); Assert.Equal("Source", generatedMigration.Resources["Source"]); Assert.Equal("Target", generatedMigration.Resources["Target"]); }
public void CreateForeignKeyOperation() { var migrationOperations = new List<MigrationOperation>(); // create dependant table Posts var createTableOperation = CreateTableOperation(); migrationOperations.Add(createTableOperation); // Add column BlogId to create the constraints if (ProviderManifest == null) ProviderManifest = new MySqlProviderManifest(Version.ToString()); TypeUsage tu = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); TypeUsage result = ProviderManifest.GetStoreType(tu); var intColumn = new ColumnModel(PrimitiveTypeKind.Int32, result) { Name = "BlogId", IsNullable = false }; var addColumnMigratioOperation = new AddColumnOperation("Posts", intColumn); migrationOperations.Add(addColumnMigratioOperation); // create constrain object var createForeignkeyOperation = new AddForeignKeyOperation(); createForeignkeyOperation.Name = "FKBlogs"; createForeignkeyOperation.DependentTable = "Posts"; createForeignkeyOperation.DependentColumns.Add("BlogId"); createForeignkeyOperation.CascadeDelete = true; createForeignkeyOperation.PrincipalTable = "Blogs"; createForeignkeyOperation.PrincipalColumns.Add("BlogId"); //create index to use migrationOperations.Add(createForeignkeyOperation.CreateCreateIndexOperation()); migrationOperations.Add(createForeignkeyOperation); using (BlogContext context = new BlogContext()) { if (context.Database.Exists()) context.Database.Delete(); context.Database.Create(); Assert.AreEqual(true, GenerateAndExecuteMySQLStatements(migrationOperations)); using (var conn = new MySqlConnection(context.Database.Connection.ConnectionString)) { if (conn.State == System.Data.ConnectionState.Closed) conn.Open(); // check for foreign key creation MySqlCommand query = new MySqlCommand("select Count(*) from information_schema.table_constraints where constraint_type = 'foreign key' and constraint_schema = '" + conn.Database + "' and constraint_name = 'FKBlogs'", conn); int rows = Convert.ToInt32(query.ExecuteScalar()); Assert.AreEqual(1, rows); // check for table creation query = new MySqlCommand("select Count(*) from information_schema.Tables WHERE `table_name` = 'Posts' and `table_schema` = '" + conn.Database + "' ", conn); rows = Convert.ToInt32(query.ExecuteScalar()); Assert.AreEqual(1, rows); conn.Close(); } } }
public void CreateForeignKeyOperation() { var migrationOperations = new List<MigrationOperation>(); // create dependant table Posts var createTableOperation = CreateTableOperation(); migrationOperations.Add(createTableOperation); // Add column BlogId to create the constraints if (ProviderManifest == null) ProviderManifest = new MySqlProviderManifest(Version.ToString()); TypeUsage tu = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); TypeUsage result = ProviderManifest.GetStoreType(tu); var intColumn = new ColumnModel(PrimitiveTypeKind.Int32, result) { Name = "BlogId", IsNullable = false }; var addColumnMigratioOperation = new AddColumnOperation("Posts", intColumn); migrationOperations.Add(addColumnMigratioOperation); // create constrain object var createForeignkeyOperation = new AddForeignKeyOperation(); createForeignkeyOperation.Name = "FKBlogs"; createForeignkeyOperation.DependentTable = "Posts"; createForeignkeyOperation.DependentColumns.Add("BlogId"); createForeignkeyOperation.CascadeDelete = true; createForeignkeyOperation.PrincipalTable = "Blogs"; createForeignkeyOperation.PrincipalColumns.Add("BlogId"); //create index to use migrationOperations.Add(createForeignkeyOperation.CreateCreateIndexOperation()); migrationOperations.Add(createForeignkeyOperation); using (BlogContext context = new BlogContext()) { if (context.Database.Exists()) context.Database.Delete(); context.Database.Create(); Assert.AreEqual(true, GenerateAndExecuteMySQLStatements(migrationOperations)); using (var conn = new MySqlConnection(context.Database.Connection.ConnectionString)) { if (conn.State == System.Data.ConnectionState.Closed) conn.Open(); // check for foreign key creation MySqlCommand query = new MySqlCommand("select Count(*) from information_schema.table_constraints where constraint_type = 'foreign key' and constraint_schema = '" + conn.Database + "' and constraint_name = 'FKBlogs'", conn); int rows = Convert.ToInt32(query.ExecuteScalar()); Assert.AreEqual(1, rows); // check for table creation query = new MySqlCommand("select Count(*) from information_schema.Tables WHERE `table_name` = 'Posts' and `table_schema` = '" + conn.Database + "' ", conn); rows = Convert.ToInt32(query.ExecuteScalar()); Assert.AreEqual(1, rows); conn.Close(); } // Test fix for MySqlConnection con = GetConnectionFromContext(context); con.Open(); try { MySqlCommand cmd = new MySqlCommand("show create table `posts`", con); using (MySqlDataReader r = cmd.ExecuteReader()) { r.Read(); string sql = r.GetString(1); Assert.IsTrue(sql.IndexOf( " CONSTRAINT `FKBlogs` FOREIGN KEY (`BlogId`) REFERENCES `blogs` (`BlogId`) ON DELETE CASCADE ON UPDATE CASCADE", StringComparison.OrdinalIgnoreCase) != -1); } } finally { con.Close(); } } }
public void Generate_can_output_create_table_statement() { var createTableOperation = new CreateTableOperation("Customers"); var idColumn = new ColumnModel(PrimitiveTypeKind.Int32) { Name = "I.d", IsNullable = true, IsIdentity = true }; createTableOperation.Columns.Add(idColumn); createTableOperation.Columns.Add( new ColumnModel(PrimitiveTypeKind.String) { Name = "Name", IsNullable = false }); createTableOperation.PrimaryKey = new AddPrimaryKeyOperation { Name = "MyPK" }; createTableOperation.PrimaryKey.Columns.Add(idColumn.Name); var codeGenerator = new CSharpMigrationCodeGenerator(); var addForeignKeyOperation = new AddForeignKeyOperation { DependentTable = "Customers", PrincipalTable = "Blogs", CascadeDelete = true }; addForeignKeyOperation.DependentColumns.Add("Blog.Id"); addForeignKeyOperation.PrincipalColumns.Add("Id"); var generatedMigration = codeGenerator.Generate( "Migration", new MigrationOperation[] { createTableOperation, addForeignKeyOperation, addForeignKeyOperation.CreateCreateIndexOperation() }, "Source", "Target", "Foo", "Bar"); Assert.Equal( @"namespace Foo { using System; using System.Data.Entity.Migrations; public partial class Bar : DbMigration { public override void Up() { CreateTable( ""Customers"", c => new { Id = c.Int(name: ""I.d"", identity: true), Name = c.String(nullable: false), }) .PrimaryKey(t => t.Id, name: ""MyPK"") .ForeignKey(""Blogs"", t => t.BlogId, cascadeDelete: true) .Index(t => t.BlogId); } public override void Down() { DropIndex(""Customers"", new[] { ""Blog.Id"" }); DropForeignKey(""Customers"", ""Blog.Id"", ""Blogs""); DropTable(""Customers""); } } } ", generatedMigration.UserCode); Assert.Equal( @"// <auto-generated /> namespace Foo { using System.CodeDom.Compiler; using System.Data.Entity.Migrations; using System.Data.Entity.Migrations.Infrastructure; using System.Resources; [GeneratedCode(""EntityFramework.Migrations"", """ + typeof(DbContext).Assembly().GetInformationalVersion() + @""")] public sealed partial class Bar : IMigrationMetadata { private readonly ResourceManager Resources = new ResourceManager(typeof(Bar)); string IMigrationMetadata.Id { get { return ""Migration""; } } string IMigrationMetadata.Source { get { return Resources.GetString(""Source""); } } string IMigrationMetadata.Target { get { return Resources.GetString(""Target""); } } } } ", generatedMigration.DesignerCode); Assert.Equal("cs", generatedMigration.Language); Assert.Equal(2, generatedMigration.Resources.Count); Assert.Equal("Source", generatedMigration.Resources["Source"]); Assert.Equal("Target", generatedMigration.Resources["Target"]); }