不为实体框架中的存储过程生成脚本

时间:2021-09-03 21:48:37

I use Entity Framework to create database scripts using the following code:

我使用Entity Framework使用以下代码创建数据库脚本:

var dbScript = ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript();

I use the MapToStoredProcedure() method in my entity mapping class. This code generated a CREATE TABLE sql script only and does not generate a SQL script for the stored procedure.

我在实体映射类中使用MapToStoredProcedure()方法。此代码仅生成CREATE TABLE sql脚本,并且不为存储过程生成SQL脚本。

How can I generate a SQL script for the stored procedure?

如何为存储过程生成SQL脚本?

1 个解决方案

#1


3  

ObjectContext.CreateDatabaseScript() is an old code path as database creation now uses the Migrations pipeline to create schema. For this reason, CreateDatabaseScript doesn't know how to process stored procedures. Use this code :

ObjectContext.CreateDatabaseScript()是一个旧的代码路径,因为数据库创建现在使用迁移管道来创建模式。因此,CreateDatabaseScript不知道如何处理存储过程。使用此代码:

var configuration = new DbMigrationsConfiguration
{
    AutomaticMigrationsEnabled = true,
    ContextType = typeof(MyContext),
    MigrationsAssembly = typeof(MyContext).Assembly
};

var migrator = new DbMigrator(configuration);

var scriptor = new MigratorScriptingDecorator(migrator);
string script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null);

Console.WriteLine(script);

#1


3  

ObjectContext.CreateDatabaseScript() is an old code path as database creation now uses the Migrations pipeline to create schema. For this reason, CreateDatabaseScript doesn't know how to process stored procedures. Use this code :

ObjectContext.CreateDatabaseScript()是一个旧的代码路径,因为数据库创建现在使用迁移管道来创建模式。因此,CreateDatabaseScript不知道如何处理存储过程。使用此代码:

var configuration = new DbMigrationsConfiguration
{
    AutomaticMigrationsEnabled = true,
    ContextType = typeof(MyContext),
    MigrationsAssembly = typeof(MyContext).Assembly
};

var migrator = new DbMigrator(configuration);

var scriptor = new MigratorScriptingDecorator(migrator);
string script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null);

Console.WriteLine(script);