2013-01-03 19 views
18

私はデータベースを作成し、最初のマイグレーションを行いました(単なるテーブルの追加)。今、SQLを作成してManagement Studioで実行することで、追加したストアドプロシージャをいくつか追加します。しかし、これらのストアドプロシージャを可能な限り移行に含めて保存して、それらを保存し、それらに対してUpまたはDownメソッドを実行できるようにしたいと思います。これが可能なのか、そうであればどのような構文を使用する必要がありますか?または、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; 
         ");    
        } 
    } 
    
関連する問題