2013-01-03 41 views
18

我刚刚创建了一个数据库并完成了我的第一次迁移(只是一个简单的表格添加)。现在我想添加一些存储过程,这些存储过程是通过编写sql并在Management Studio中执行来添加的。但我希望在迁移时尽可能包含这些存储过程,以便保存它们,并且可以针对它们运行向上或向下方法。这是可能的,如果是这样,需要使用什么语法?或者我只需要使用Management Studio添加/编辑/删除它们?代码优先迁移和存储过程

+0

可能欺骗http://stackoverflow.com/questions/7667630/can-you-create-sql-views-stored-procedure-using-entity- framework-4-1-code-firs –

回答

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 ... 
    "; 
} 
+0

如果您正在修改过程,因为它是在先前的迁移中创建的,那么您需要关闭该过程?你不会只是将程序撤回到它原来的状态,不管程序看起来像什么...... – Ryan

+1

@Ryan更新,更清楚地显示改变与创建/删除 – NKeddie

+0

我喜欢更具体的方法http://stackoverflow.com/a/27711523/344895 – Madman

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; 
         ");    
        } 
    }