这就是我最终的结果。我猜这不是最好的解决方案,但至少是一个可行的解决方案,直到进一步的建议:-)
由于我没有管理它以从 EdmModel 类或 DBModel 生成有效的 SQL,我创建了一个 DbMigration 类,它现在创建表。不像 ModelBuilder 方法那么聪明,但正如我所提到的 - 工作:
internal class CreateSingleTableMigration: DbMigration
{
public override void Up()
{
CreateTable(
"\"SCHEMA\".\"TMPEntityTable\"",
c => new
{
// Single column
EntityId= c.String(nullable: false, maxLength: 150)
// Further columns go in here ...
})
.PrimaryKey(t => new { t.EntityId /*Combined PK goes here*/ });
}
}
现在的问题是,如何使用选定的 IDbProvider 生成并执行有效的 SQL。由于我想重用现有的 DbContext,所有必需的信息都可以在这个类中获得。
// If database exists
if (context.Database.Exists())
{
// Get the corresponding DbProviderServices the Provider of the current Connection has to offer
var serv = DbProviderServices.GetProviderServices(context.Database.Connection);
// Get the provider manifest token
var tkn = serv.GetProviderManifestToken(context.Database.Connection);
// Get the PropertyInfo of the internal "Operations" property
var prop = typeof(InitializeHistorizationMigration)
.GetProperty("Operations", BindingFlags.NonPublic | BindingFlags.Instance);
// If Property "operations" was found
if (prop != null)
{
// get SQL Generator for the current provider
var generator = new Configuration().GetSqlGenerator(FWLPDataContext.Provider);
// Create instance of the CreateTable-Migration
var tmpMig= new CreateSingleTableMigration();
// Apply it, so property "Operation" is filled
tmpMig.Up();
// Get the operations as a List of single MigrationOperation
var operations = (prop.GetValue(tmpMig) as IEnumerable<MigrationOperation>).ToList();
// Generate SQL and run it against the database using the DbContext Connection
foreach (var item in generator.Generate(operations, tkn))
{
context.Database.ExecuteSqlCommand(item.Sql);
}
}
}
使用 postgres 执行 SQL 脚本产生错误:SqlState: 3F000
MessageText:没有选择要在其中创建的架构
这就是我在 CreateSingleTableMigration 类中添加架构的原因,在连接字符串中设置搜索路径没有帮助。
如果您对如何优化有更好的方法或想法,请告诉我!