DbMigration.SqlFile difference in base directory

We use the new DbMigration.SqlFile method in EF Migrations 6.1.2 to run the script migration in our migration. According to the documentation , the file should be relative to the current AppDomain BaseDirectory. We included these files in the project and installed them for copying to the output directory. Locally, all this is normal. They are output to the bin directory and work fine.

When you deploy software to a server running IIS, the migration fails because it unexpectedly expects the files to belong to the root. When I copy them there, migration works.

How can I use DbMigration.SqlFile so that it runs correctly both locally and on the server?

+6
source share
1 answer

The SqlFile method uses CurrentDomain.BaseDirectory if a relative path is specified. The workaround is to map the path yourself and give an absolute path to the method. The solution will look like this:

 var sqlFile = "MigrationScripts/script1.sql"; var filePath = Path.Combine(GetBasePath(), sqlFile); SqlFile(filePath); public static string GetBasePath() { if(System.Web.HttpContext.Current == null) return AppDomain.CurrentDomain.BaseDirectory; else return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"bin"); } 

BasePath solution taken from: Why doesn't AppDomain.CurrentDomain.BaseDirectory contain a β€œbin” in an asp.net application?

+4
source

Source: https://habr.com/ru/post/981595/


All Articles