2013-01-03 32 views
18

Tôi vừa tạo ra một cơ sở dữ liệu và thực hiện di chuyển đầu tiên của tôi (chỉ cần thêm một bảng đơn giản). Bây giờ tôi muốn thêm một số thủ tục được lưu trữ mà tôi vừa thêm bằng cách viết sql và thực hiện nó trong Management Studio. Nhưng tôi muốn bao gồm các thủ tục được lưu trữ này nếu có thể trong quá trình di chuyển để chúng được lưu và tôi có thể chạy phương thức Up hoặc Down đối với chúng. Điều này có thể và nếu như vậy cú pháp nào cần được sử dụng? Hoặc tôi sẽ phải thêm/chỉnh sửa/xóa chúng bằng Management Studio?Mã số đầu tiên di chuyển và lưu trữ thủ tục

+0

thể dupe http://stackoverflow.com/questions/7667630/can-you-create-sql-views-stored-procedure-using-entity- framework-4-1-code-firs –

Trả lời

23

Tôi đã làm điều này như vậy ...

Trong lớp di dân hiện nay -

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

Còn nếu bạn đang thay đổi quy trình vì nó đã được tạo ra trong lần di chuyển trước và sau đó bạn cần phải đi xuống? Bạn sẽ không chỉ DROP thủ tục nó sẽ phải quay trở lại trạng thái ban đầu của nó là bất cứ thủ tục như thế nào trước đây ... – Ryan

+1

@Ryan cập nhật để hiển thị rõ ràng hơn một thay đổi so với tạo/thả – NKeddie

+0

Tôi thích cách tiếp cận cụ thể hơn http://stackoverflow.com/a/27711523/344895 – Madman

7

Tôi đang sử dụng EF6 và lớp DbMigration cung cấp phương pháp để Tạo/Alter/Xóa lưu trữ thủ tục

  • Tạo một thủ tục lưu trữ mới

    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");     
        } 
    } 
    
  • Sửa đổi một stored procedure

    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; 
         ");    
        } 
    } 
    
Các vấn đề liên quan