DbMigration.SqlFile difference in base directory

c# ef-migrations entity-framework-6


We are running a migration script in our migration utilizing the new DbMigration.SqlFile function in EF Migrations 6.1.2. The file must be related to the active AppDomain BaseDirectory, according to the evidence. These files are part of the project, and we've got them set to copy to the output directory. Everything works perfectly locally. They function well and produce output in the bin directory.

However, the migration fails when the software is deployed to a server running IIS because it suddenly wants the files to be relative to the root. The migration takes place after I copy them there.

How can I use DbMigration.SqlFile such that it functions properly on both the server and locally?

1/27/2015 7:17:04 AM

Accepted Answer

The SqlFile method uses theCurrentDomain.BaseDirectory when a relative path is specified. Making your own map of the path and giving it an absolute path is a workaround. A possible answer would be as follows:

var sqlFile = "MigrationScripts/script1.sql";
var filePath = Path.Combine(GetBasePath(), sqlFile);

public static string GetBasePath()          
    if(System.Web.HttpContext.Current == null) return AppDomain.CurrentDomain.BaseDirectory; 
    else return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"bin");

Solution for BasePath obtained from: Why use AppDomain. Does the asp.net application's CurrentDomain.BaseDirectory contain "bin"?

5/23/2017 11:53:24 AM

Popular Answer

From within the migration, we're using it in the following way:SqlFile(@"..\..\Sql\views\SomeView.sql");

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow