I am using EF 6 Code-base migration. I want to read the file from Configuration.cs Seed() method to insert as default data. I am using HostingEnvironment.MapPath() as below:
string _homeContent = System.IO.File.ReadAllText( HostingEnvironment.MapPath(@"~/Data/DefaultContents/Home.html"));
But HostingEnvironment.MapPath return null value when I run the Update-database command from Package-Management Console .
Running Seed method.
System.ArgumentNullException: Value cannot be null.
Parameter name: path at System.IO.File.ReadAllText(String path)
Kindly advise how to read the physical file from Configuration.cs Seed() method.
May I answer by myself.
Server.MapPath and HostingEnvironment.MapPath is not available in this time because HttpContext object is not ready yet.
AppDomain.CurrentDomain.BaseDirectory return "..\\your project directory\\bin"
Eg C:\\YourSolutionDirectory\\YourProjectDirectory\\bin
But we need to go up from " bin " folder, so that using Path.Combine() method and get exact file path.
string homeFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", @"Data\DefaultContents\Home.html");
Result: C:\\YourSolutionDirectory\\YourProjectDirectory\\Data\\DefaultContents\\Home.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.