use*_*406 27 sql-server asp.net-mvc entity-framework code-first
我刚刚创建了一个数据库并完成了我的第一次迁移(只是一个简单的表添加).现在我想添加一些我刚刚添加的存储过程,通过编写sql并在Management Studio中执行它.但是我希望在迁移中包含这些存储过程,以便保存它们,并且我可以针对它们运行Up或Down方法.这是可能的,如果是这样,需要使用什么语法?或者我只需要使用Management Studio添加/编辑/删除它们?
NKe*_*die 26
我这样做了......
在当前的迁移类中 -
public partial class MyMigration : DbMigration
{
public override void Up()
{
... other table creation logic
// This command executes the SQL you have written
// to create the stored procedures
Sql(InstallScript);
// or, to alter stored procedures
Sql(AlterScript);
}
public override void Down()
{
... other table removal logic
// This command executes the SQL you have written
// to drop the stored procedures
Sql(UninstallScript);
// or, to rollback stored procedures
Sql(RollbackScript);
}
private const string InstallScript = @"
CREATE PROCEDURE [dbo].[MyProcedure]
... SP logic here ...
";
private const string UninstallScript = @"
DROP PROCEDURE [dbo].[MyProcedure];
";
// or for alters
private const string AlterScript = @"
ALTER PROCEDURE [dbo].[AnotherProcedure]
... Newer SP logic here ...
";
private const string RollbackScript = @"
ALTER PROCEDURE [dbo].[AnotherProcedure]
... Previous / Old SP logic here ...
";
}
Run Code Online (Sandbox Code Playgroud)
Tho*_*mas 11
我正在使用EF6,DbMigration
该类提供了创建/更改/删除存储过程的方法
创建一个新的存储过程
public partial class MyFirstMigration : DbMigration
{
public override void Up()
{
// Create a new store procedure
CreateStoredProcedure("dbo.DequeueMessages"
// These are stored procedure parameters
, c => new{
MessageCount = c.Int()
},
// Here is the stored procedure body
@"
SET NOCOUNT ON;
SELECT TOP (@MessageCount)
*
FROM
dbo.MyTable;
");
}
public override void Down()
{
// Delete the stored procedure
DropStoredProcedure("dbo.DequeueMessages");
}
}
Run Code Online (Sandbox Code Playgroud)修改存储过程
public partial class MySecondMigration : DbMigration
{
public override void Up()
{
// Modify an existing stored procedure
AlterStoredProcedure("dbo.DequeueMessages"
// These are new stored procedure parameters
, c => new{
MessageCount = c.Int(),
StatusId = c.Int()
},
// Here is the new stored procedure body
@"
SET NOCOUNT ON;
SELECT TOP (@MessageCount)
*
FROM
dbo.MyTable
WHERE
StatusId = @StatusId;
");
}
public override void Down()
{
// Rollback to the previous stored procedure
// Modify an existing stored procedure
AlterStoredProcedure("dbo.DequeueMessages"
// These are old stored procedure parameters
, c => new{
MessageCount = c.Int()
},
// Here is the old stored procedure body
@"
SET NOCOUNT ON;
SELECT TOP (@MessageCount)
*
FROM
dbo.MyTable;
");
}
}
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
15673 次 |
最近记录: |