2013-01-03 39 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; 
         ");    
        } 
    }