本文整理汇总了C#中System.Data.Entity.Migrations.Design.CSharpMigrationCodeGenerator.Generate方法的典型用法代码示例。如果您正苦于以下问题:C# CSharpMigrationCodeGenerator.Generate方法的具体用法?C# CSharpMigrationCodeGenerator.Generate怎么用?C# CSharpMigrationCodeGenerator.Generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.Entity.Migrations.Design.CSharpMigrationCodeGenerator
的用法示例。
在下文中一共展示了CSharpMigrationCodeGenerator.Generate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UniquifyName_should_return_unique_name_when_conflict
public void UniquifyName_should_return_unique_name_when_conflict()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var @namespace = "Migrations";
var generatedMigration1
= codeGenerator.Generate(
"201108162311111_Migration",
new MigrationOperation[] { },
"Source",
"Target",
@namespace,
"Migration");
var generatedMigration2
= codeGenerator.Generate(
"201108162311111_Migration1",
new MigrationOperation[] { },
"Source",
"Target",
@namespace,
"Migration1");
var migrationAssembly
= new MigrationAssembly(
new MigrationCompiler("cs")
.Compile(
@namespace,
generatedMigration1,
generatedMigration2),
@namespace);
Assert.Equal("Migration2", migrationAssembly.UniquifyName("Migration"));
}
示例2: Generate_can_output_create_procedure_operations
public void Generate_can_output_create_procedure_operations()
{
var createProcedureOperation
= new CreateProcedureOperation("Foo", "SELECT ShinyHead\r\nFROM Pilkingtons");
createProcedureOperation.Parameters.Add(
new ParameterModel(PrimitiveTypeKind.String)
{
Name = "P'",
DefaultValue = "Bar",
IsOutParameter = true
});
var codeGenerator = new CSharpMigrationCodeGenerator();
var generatedMigration
= codeGenerator.Generate(
"Migration",
new MigrationOperation[]
{
createProcedureOperation
},
"Source",
"Target",
"Foo",
"Bar");
Assert.Equal(
@"namespace Foo
{
using System;
using System.Data.Entity.Migrations;
public partial class Bar : DbMigration
{
public override void Up()
{
CreateStoredProcedure(
""Foo"",
p => new
{
P = p.String(name: ""P'"", defaultValue: ""Bar"", outParameter: true),
},
body:
@""SELECT ShinyHead
FROM Pilkingtons""
);
}
public override void Down()
{
DropStoredProcedure(""Foo"");
}
}
}
",
generatedMigration.UserCode);
}
示例3: Generate_can_output_drop_primary_key_with_explicit_name
public void Generate_can_output_drop_primary_key_with_explicit_name()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var dropPrimaryKeyOperation
= new DropPrimaryKeyOperation
{
Table = "T",
Name = "PK"
};
dropPrimaryKeyOperation.Columns.Add("c1");
dropPrimaryKeyOperation.Columns.Add("c2");
var generatedMigration
= codeGenerator.Generate(
"Migration",
new MigrationOperation[] { dropPrimaryKeyOperation },
"Source",
"Target",
"Foo",
"Bar");
Assert.Equal(
@"namespace Foo
{
using System;
using System.Data.Entity.Migrations;
public partial class Bar : DbMigration
{
public override void Up()
{
DropPrimaryKey(""T"", ""PK"");
}
public override void Down()
{
AddPrimaryKey(""T"", new[] { ""c1"", ""c2"" }, name: ""PK"");
}
}
}
",
generatedMigration.UserCode);
}
示例4: Generate_should_not_produce_lines_that_are_too_long_for_the_compiler
public void Generate_should_not_produce_lines_that_are_too_long_for_the_compiler()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var generatedMigration
= codeGenerator.Generate(
"Migration",
new MigrationOperation[] { },
new string('a', 10000),
"Target",
"Foo",
"Bar");
using (var stringReader = new StringReader(generatedMigration.DesignerCode))
{
string line;
while ((line = stringReader.ReadLine()) != null)
{
Assert.True(line.Length <= 1100);
}
}
}
示例5: Can_generate_AlterTableAnnotations_with_annotations
public void Can_generate_AlterTableAnnotations_with_annotations()
{
var operation = new AlterTableOperation(
"Customers",
new Dictionary<string, AnnotationValues>
{
{ "AT1", new AnnotationValues(null, "VT1") },
{
CollationAttribute.AnnotationName,
new AnnotationValues(
new CollationAttribute("At a reasonable volume..."),
new CollationAttribute("While I'm collating..."))
},
{ "AT2", new AnnotationValues(null, "VT2") }
});
var idColumn = new ColumnModel(PrimitiveTypeKind.Int32)
{
Name = "I.d",
IsNullable = true,
IsIdentity = true,
Annotations =
new Dictionary<string, AnnotationValues>
{
{ "A1", new AnnotationValues(null, "V1") },
{ "A2", new AnnotationValues(null, "V2") }
}
};
operation.Columns.Add(idColumn);
operation.Columns.Add(
new ColumnModel(PrimitiveTypeKind.String)
{
Name = "Name",
IsNullable = false,
Annotations =
new Dictionary<string, AnnotationValues>
{
{
CollationAttribute.AnnotationName,
new AnnotationValues(
new CollationAttribute("At a reasonable volume..."),
new CollationAttribute("While I'm collating..."))
}
}
});
var operations = new[] { operation };
var generator = new CSharpMigrationCodeGenerator();
generator.AnnotationGenerators[CollationAttribute.AnnotationName] = () => new CollationCSharpCodeGenerator();
var generatedMigration = generator.Generate("Migration", operations, "Source", "Target", "MyNamespace", "MyMigration");
Assert.Equal(
@"namespace MyNamespace
{
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.Migrations;
using System.Data.Entity.TestHelpers;
public partial class MyMigration : DbMigration
{
public override void Up()
{
AlterTableAnnotations(
""Customers"",
c => new
{
Id = c.Int(name: ""I.d"", identity: true,
annotations: new Dictionary<string, AnnotationValues>
{
{
""A1"",
new AnnotationValues(oldValue: null, newValue: ""V1"")
},
{
""A2"",
new AnnotationValues(oldValue: null, newValue: ""V2"")
},
}),
Name = c.String(nullable: false,
annotations: new Dictionary<string, AnnotationValues>
{
{
""Collation"",
new AnnotationValues(oldValue: new CollationAttribute(""At a reasonable volume...""), newValue: new CollationAttribute(""While I'm collating...""))
},
}),
},
annotations: new Dictionary<string, AnnotationValues>
{
{
""AT1"",
new AnnotationValues(oldValue: null, newValue: ""VT1"")
},
{
""AT2"",
//.........这里部分代码省略.........
示例6: 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""; }
}
//.........这里部分代码省略.........
示例7: MigrationIds_should_not_return_migration_in_wrong_namespace
public void MigrationIds_should_not_return_migration_in_wrong_namespace()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var @namespace = "CorrectNamespace";
var generatedMigration
= codeGenerator.Generate(
"201108162311111_Migration",
new MigrationOperation[] { },
"Source",
"Target",
@namespace,
"Migration");
var migrationAssembly = new MigrationAssembly(
new MigrationCompiler("cs").Compile(@namespace, generatedMigration),
"WrongNamespace");
Assert.Equal(0, migrationAssembly.MigrationIds.Count());
}
示例8: Generate_can_output_drop_column
public void Generate_can_output_drop_column()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var dropColumnOperation = new DropColumnOperation("Customers", "Foo");
var generatedMigration
= codeGenerator.Generate(
"Migration",
new MigrationOperation[] { dropColumnOperation },
"Source",
"Target",
"Foo",
"Bar");
Assert.Equal(
@"namespace Foo
{
using System;
using System.Data.Entity.Migrations;
public partial class Bar : DbMigration
{
public override void Up()
{
DropColumn(""Customers"", ""Foo"");
}
public override void Down()
{
}
}
}
",
generatedMigration.UserCode);
}
示例9: Generate_can_output_timestamp_column
public void Generate_can_output_timestamp_column()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var createTableOperation = new CreateTableOperation("Customers");
var column = new ColumnModel(PrimitiveTypeKind.Binary)
{
Name = "Version",
IsTimestamp = true
};
createTableOperation.Columns.Add(column);
var generatedMigration
= codeGenerator.Generate(
"Migration",
new MigrationOperation[] { createTableOperation },
"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
{
Version = c.Binary(timestamp: true),
});
}
public override void Down()
{
DropTable(""Customers"");
}
}
}
",
generatedMigration.UserCode);
}
示例10: Generate_can_output_drop_procedure_operations
public void Generate_can_output_drop_procedure_operations()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var generatedMigration
= codeGenerator.Generate(
"Migration",
new MigrationOperation[]
{
new DropProcedureOperation("Foo"),
new DropTableOperation("Bar", new CreateTableOperation("Bar"))
},
"Source",
"Target",
"Foo",
"Bar");
Assert.Equal(
@"namespace Foo
{
using System;
using System.Data.Entity.Migrations;
public partial class Bar : DbMigration
{
public override void Up()
{
DropStoredProcedure(""Foo"");
DropTable(""Bar"");
}
public override void Down()
{
CreateTable(
""Bar"",
c => new
{
});
throw new NotSupportedException(""" + Strings.ScaffoldSprocInDownNotSupported + @""");
}
}
}
",
generatedMigration.UserCode);
}
示例11: Generate_can_output_composite_add_foreign_key
public void Generate_can_output_composite_add_foreign_key()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var addForeignKeyOperation
= new AddForeignKeyOperation
{
DependentTable = "Orders",
PrincipalTable = "Customers"
};
addForeignKeyOperation.DependentColumns.Add("CustomerId1");
addForeignKeyOperation.DependentColumns.Add("CustomerId2");
addForeignKeyOperation.PrincipalColumns.Add("Id1");
addForeignKeyOperation.PrincipalColumns.Add("Id2");
var generatedMigration
= codeGenerator.Generate(
"Migration",
new MigrationOperation[] { addForeignKeyOperation },
"Source",
"Target",
"Foo",
"Bar");
Assert.Equal(
@"namespace Foo
{
using System;
using System.Data.Entity.Migrations;
public partial class Bar : DbMigration
{
public override void Up()
{
AddForeignKey(""Orders"", new[] { ""CustomerId1"", ""CustomerId2"" }, ""Customers"", new[] { ""Id1"", ""Id2"" });
}
public override void Down()
{
DropForeignKey(""Orders"", new[] { ""CustomerId1"", ""CustomerId2"" }, ""Customers"");
}
}
}
",
generatedMigration.UserCode);
}
示例12: GenerateAnnotation_checks_arguments
public void GenerateAnnotation_checks_arguments()
{
var generator = new CSharpMigrationCodeGenerator();
Assert.Equal(
"alterTableOperation",
Assert.Throws<ArgumentNullException>(() => generator.Generate(null, new IndentedTextWriter(new Mock<TextWriter>().Object))).ParamName);
Assert.Equal(
"writer",
Assert.Throws<ArgumentNullException>(() => generator.Generate(new AlterTableOperation("N", null), null)).ParamName);
}
示例13: Generate_can_process_null_source_model
public void Generate_can_process_null_source_model()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var generatedMigration = codeGenerator.Generate(
"Migration",
new MigrationOperation[] { },
null,
"Target",
"Foo",
"Bar");
Assert.Equal(
@"// <auto-generated />
namespace Foo
{
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.Infrastructure;
using System.Resources;
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 null; }
}
string IMigrationMetadata.Target
{
get { return Resources.GetString(""Target""); }
}
}
}
",
generatedMigration.DesignerCode);
Assert.Equal(1, generatedMigration.Resources.Count);
Assert.Equal("Target", generatedMigration.Resources["Target"]);
}
示例14: MigrationIds_should_return_id_when_migration_is_valid
public void MigrationIds_should_return_id_when_migration_is_valid()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var @namespace = "Migrations";
var generatedMigration
= codeGenerator.Generate(
"201108162311111_Migration",
new MigrationOperation[] { },
"Source",
"Target",
@namespace,
"Migration");
var migrationAssembly = new MigrationAssembly(
new MigrationCompiler("cs").Compile(@namespace, generatedMigration),
@namespace);
Assert.Equal(1, migrationAssembly.MigrationIds.Count());
}
示例15: Generate_can_output_move_procedure_statement
public void Generate_can_output_move_procedure_statement()
{
var codeGenerator = new CSharpMigrationCodeGenerator();
var generatedMigration
= codeGenerator.Generate(
"Migration",
new[] { new MoveProcedureOperation("Insert_Customers", "foo") },
"Source",
"Target",
"Foo",
"Bar");
Assert.Equal(
@"namespace Foo
{
using System;
using System.Data.Entity.Migrations;
public partial class Bar : DbMigration
{
public override void Up()
{
MoveStoredProcedure(name: ""Insert_Customers"", newSchema: ""foo"");
}
public override void Down()
{
MoveStoredProcedure(name: ""foo.Insert_Customers"", newSchema: null);
}
}
}
",
generatedMigration.UserCode);
}