Stored Scripting Function in Code First Entity Framework

I am using manual database migration.

Currently, whenever there is any new stored procedure or function, I create a transfer using the following approach:

Add-Migration CreatefnGenerateRequestCode

Now the class has CreatefnGenerateRequestCodebeen framed.

I have two options (as far as I know) that either I can write inline create procedureSQL code, or I can read the SQL script from the folder.

I don’t like the built-in approach, because as the process gets more complicated I don’t want to keep string concatenation.

So, I found an approach thanks to which I can load the embedded resource using dll.

Now i am creating a script file

I have a folder with a name Scriptsthat stores fnGenerateRequestCode.sql, and now I have enclosed it in a DLL.

, , dll:

private string GetFileCode(string resourceName)
{
    var assembly = Assembly.GetExecutingAssembly();
    string result;

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

    return result;
}

:

public override void Up()
{
    var resourceName = "CBA.RBS.CEDS.Data.Scripts.fnGenerateRequestCode.sql";
    var inlinecode = this.GetFileCode(resourceName);

    if (string.IsNullOrWhiteSpace(inlinecode)) { }
    else
    {
        Sql(inlinecode);
    }
}

public override void Down()
{
    Sql("Drop function dbo.fnGenerateRequestCode");
}

, , script , DLL, , , dll , .

, script Update-Database.

, , - Copy to Output Directory, , , :

PM > -Verbose SQL . : [201410230234306_CreateFullBackupProcedure]. : 201410230234306_CreateFullBackupProcedure. System.IO.DirectoryNotFoundException: 'C:\Users\Malham\AppData\Local\Assembly\DL3\G787MA29.K3X\JL3MCKQY.NGR\8b8db792\7af63578_6beecf01\Scripts\spFullBackup.sql'.

- :

  • dll, . , ?
  • ?
+4

All Articles