本文整理汇总了C#中System.Data.Entity.Migrations.Model.AddForeignKeyOperation.CreateCreateIndexOperation方法的典型用法代码示例。如果您正苦于以下问题:C# AddForeignKeyOperation.CreateCreateIndexOperation方法的具体用法?C# AddForeignKeyOperation.CreateCreateIndexOperation怎么用?C# AddForeignKeyOperation.CreateCreateIndexOperation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.Entity.Migrations.Model.AddForeignKeyOperation
的用法示例。
在下文中一共展示了AddForeignKeyOperation.CreateCreateIndexOperation方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateCreateIndexOperation_should_return_corresponding_create_index_operation
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());
}
示例2: Generate_can_output_create_table_statement
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"")
//.........这里部分代码省略.........
示例3: CreateForeignKeyOperation
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();
}
}
}
示例4: CreateForeignKeyOperation
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();
}
}
}
示例5: Generate_can_output_create_table_statement
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""; }
}
//.........这里部分代码省略.........