我刚刚创建了一个数据库并完成了我的第一次迁移(只是一个简单的表格添加)。现在我想添加一些存储过程,这些存储过程是通过编写sql并在Management Studio中执行来添加的。但我希望在迁移时尽可能包含这些存储过程,以便保存它们,并且可以针对它们运行向上或向下方法。这是可能的,如果是这样,需要使用什么语法?或者我只需要使用Management Studio添加/编辑/删除它们?代码优先迁移和存储过程
18
A
回答
23
我做这个是这样的...
在目前的移民类 -
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 ...
";
}
7
我使用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"); } }
修改存储过程
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; "); } }
相关问题
- 1. EntityFramework5代码优先迁移
- 2. 代码优先迁移MissingManifestResourceException
- 3. EF 6代码优先迁移跳过
- 4. LocalDb的代码优先迁移和DataDirectory
- 5. DDD和EF代码优先迁移
- 6. 在执行代码优先迁移过程中上传图片
- 7. 代码优先迁移过程:我缺少什么部分?
- 8. EF代码优先迁移不编译
- 9. 被代码优先迁移困惑
- 10. EF 5 - 代码优先迁移
- 11. 的EntityFramework代码优先迁移
- 12. 如何启用代码优先迁移
- 13. EF代码优先4.3:迁移/种子
- 14. 不使用代码优先迁移
- 15. 代码优先迁移未执行
- 16. EF5代码优先 - 迁移列类型
- 17. WebSecurity.InitializeDatabaseConnection不配合代码优先迁移
- 18. 实体框架4 - 代码优先和存储过程
- 19. 实体框架代码优先和存储过程
- 20. 从EF自动迁移到代码优先迁移
- 21. 代码优先迁移 - 如何回滚首次迁移
- 22. EF6代码优先迁移:迁移事件
- 23. 通过Visual Studio 2013部署时无代码优先迁移
- 24. EF4.1代码优先:存储过程与输出参数
- 25. EF(6.1)代码优先存储过程问题
- 26. EF 4.1代码优先:执行存储过程并返回IDENT
- 27. EF 6代码优先:如何更新存储过程?
- 28. 使用实体框架代码优先创建存储过程?
- 29. 存储过程在代码优先中引发Guids错误
- 30. 代码优先存储过程不同表
可能欺骗http://stackoverflow.com/questions/7667630/can-you-create-sql-views-stored-procedure-using-entity- framework-4-1-code-firs –