public void CanMapByteTypeToUTinyInt() { using (MySqlConnection connection = new MySqlConnection(GetConnectionString(true))) { MySqlProviderManifest pm = new MySqlProviderManifest(Version.ToString()); TypeUsage tu = TypeUsage.CreateDefaultTypeUsage( PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Byte)); TypeUsage result = pm.GetStoreType(tu); Assert.AreEqual("utinyint", result.EdmType.Name); } }
public void AddColumnOperationMigration() { var migrationOperations = new List<MigrationOperation>(); 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 = "TotalPosts", IsNullable = false }; var addColumnMigratioOperation = new AddColumnOperation("Blogs", intColumn); migrationOperations.Add(addColumnMigratioOperation); using (BlogContext context = new BlogContext()) { if (context.Database.Exists()) context.Database.Delete(); context.Database.Create(); using (MySqlConnection conn = new MySqlConnection(context.Database.Connection.ConnectionString)) { if (conn.State == System.Data.ConnectionState.Closed) conn.Open(); Assert.AreEqual(true, GenerateAndExecuteMySQLStatements(migrationOperations)); MySqlCommand query = new MySqlCommand("Select Column_name, Is_Nullable, Data_Type from information_schema.Columns where table_schema ='" + conn.Database + "' and table_name = 'Blogs' and column_name ='TotalPosts'" , conn); MySqlDataReader reader = query.ExecuteReader(); while (reader.Read()) { Assert.AreEqual("TotalPosts", reader[0].ToString()); Assert.AreEqual("NO", reader[1].ToString()); Assert.AreEqual("int", reader[2].ToString()); } reader.Close(); conn.Close(); } } }
public void TestingMaxLengthFacet() { using (MySqlConnection connection = new MySqlConnection(GetConnectionString(true))) { MySqlProviderManifest pm = new MySqlProviderManifest(Version.ToString()); TypeUsage tu = TypeUsage.CreateStringTypeUsage( PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String), false, false); TypeUsage result = pm.GetStoreType(tu); Assert.AreEqual("longtext", result.EdmType.Name); tu = TypeUsage.CreateStringTypeUsage( PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String), false, false, Int32.MaxValue); result = pm.GetStoreType(tu); Assert.AreEqual("longtext", result.EdmType.Name); tu = TypeUsage.CreateStringTypeUsage( PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String), false, false, 70000); result = pm.GetStoreType(tu); Assert.AreEqual("mediumtext", result.EdmType.Name); } }
/// <summary> /// Creates a table named Posts /// and columns int PostId, string Title, string Body /// </summary> /// <returns></returns> private CreateTableOperation CreateTableOperation() { TypeUsage tu; TypeUsage result; if (ProviderManifest == null) ProviderManifest = new MySqlProviderManifest(Version.ToString()); var createTableOperation = new CreateTableOperation("Posts"); //Column model for int IdPost tu = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)); result = ProviderManifest.GetStoreType(tu); var intColumn = new ColumnModel(PrimitiveTypeKind.Int32, result) { Name = "PostId", IsNullable = false, IsIdentity = true }; createTableOperation.Columns.Add(intColumn); //Column model for string tu = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)); result = ProviderManifest.GetStoreType(tu); var stringColumnTitle = new ColumnModel(PrimitiveTypeKind.String, result) { Name = "Title", IsNullable = false }; var stringColumnBody = new ColumnModel(PrimitiveTypeKind.String, result) { Name = "Body", IsNullable = true }; createTableOperation.Columns.Add(stringColumnTitle); createTableOperation.Columns.Add(stringColumnBody); var primaryKey = new AddPrimaryKeyOperation(); primaryKey.Columns.Add("PostId"); createTableOperation.PrimaryKey = primaryKey; return createTableOperation; }
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(); } } }