簡體   English   中英

EF 6代碼優先與自定義存儲過程

[英]EF 6 code-first with custom stored procedure

我正在使用Code-First方法創建一個MVC 5應用程序,但我還在SQL Server數據庫上創建了一些存儲過程,有沒有辦法在創建數據庫時在c#中生成這些存儲過程,可能是通過執行sql腳本,如果是這樣我應該在哪里這樣做?

我會使用代碼遷移。

在您的Nuget Package Manager中,您可以通過鍵入來設置空白遷移

add-migration AddMyStoredProcedure

這應該生成一個像這樣的空類

public partial class AddMyStoredProcedure : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}

您需要做的就是添加您的存儲過程(請記住在Down方法中刪除存儲過程,以防將來需要回滾遷移)。

    public partial class AddMyStoredProcedure : DbMigration
{
    public override void Up()
    {
        Sql(@"
            CREATE PROCEDURE dbo.GetMyAddress
            AS
            SELECT * FROM Person.Address");
    }

    public override void Down()
    {
        Sql("DROP PROCEDURE dbo.GetMyAddress");
    }
}

最后更新您的數據庫

update-database

遲到的答案,但也許有人會得到這個問題的答案

我有很多viewsfunctionsstored procedures來處理我的項目,我使用的解決方案如下:

  1. 在項目中創建一個sql文件,刪除它們存在時的所有視圖函數以及過程 ,例如將其命名為drop.sql
  2. 為每個viewfunction以及stored procedure創建項目中的單獨sql文件。
  3. 將所有sql文件標記為Embedded Resource ,右鍵單擊文件,然后單擊屬性,然后選擇Build Action,選擇Embedded Resource

    YourFile.sql =>右鍵單擊=> Properties => Build Action,選擇Embedded Resource

  4. 在遷移種子函數中,使用ExecuteSqlCommand ,您需要一種方法來允許您讀取這些文件以及所需的所有代碼。

drop.sql結構

-- your views
if object_id('dbo.[YourViewName1]') is not null
    drop view dbo.[YourViewName1]
if object_id('dbo.[YourViewName2]') is not null
    drop view dbo.[YourViewName2]
-- your functions 
if object_id('dbo.[Function1]') is not null
    drop function dbo.[Function1]
if object_id('dbo.[Function2]') is not null
    drop function dbo.[Function2]
-- your procedures
if object_id('dbo.[Procedure1]') is not null
    drop procedure dbo.[Procedure1]
if object_id('dbo.[Procedure2]') is not null
    drop procedure dbo.[Procedure2]

view.sql或function.sql或procedure.sql結構

create view View1 
as
  select Field1,Field2,...Fieldn
  from Table1
  inner join Table2 on Id1 = FId2  
  inner join TableN on IdI = IdJ

遷移種子方法

我假設您在項目的Migrations文件夾中的Sql文件夾中創建了所有sql文件

protected override void Seed(YourContext context)
{
    context.Database
            .ExecuteSqlCommand(Load("YourProject.Migrations.Sql.drop.sql"));
    context.Database
            .ExecuteSqlCommand(Load("YourProject.Migrations.Sql.view1.sql"));
    context.Database
            .ExecuteSqlCommand(Load("YourProject.Migrations.Sql.view2.sql"));
    context.Database
            .ExecuteSqlCommand(Load("YourProject.Migrations.Sql.function1.sql"));
    context.Database
            .ExecuteSqlCommand(Load("YourProject.Migrations.Sql.function2.sql"));
    context.Database
            .ExecuteSqlCommand(Load("YourProject.Migrations.Sql.procedure1.sql"));
    context.Database
            .ExecuteSqlCommand(Load("YourProject.Migrations.Sql.procedure2.sql"));
}

最后是Load方法

private static string Load(string name)
{
    var assembly = Assembly.GetExecutingAssembly();

    using (Stream stream = assembly.GetManifestResourceStream(name))
    using (StreamReader reader = new StreamReader(stream))
    {
        string result = reader.ReadToEnd();
        return result;
    }
}

此解決方案的好處是它每次都會運行,並且您將確保如果有任何問題(例如,一段時間后您更改了字段名稱或刪除了在視圖或函數或過程中使用的表如果不記得必須更新程序,就會出現錯誤,如果啟用了自動遷移,則可以修復。

希望對你有幫助

您可能需要使用遷移來處理它。 可以找到一個好看的解決方案https://stackoverflow.com/a/15171900/119262 使用資源,但我相信如果你不想沿着資源路徑走,你可以用同樣的方式讀取.sql文件中的文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM