Reputation: 4695
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.
Upvotes: 1
Views: 576
Reputation: 4695
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"
E.g. 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
Upvotes: 2