9

Why is filePath null? Any ideas on how to get the relative filePath?

internal sealed class Configuration : DbMigrationsConfiguration<MvcProject.Models.FileDb>
{
    public Configuration()
    {
        // code here is not relevant to question
    }
    protected override void Seed(MvcProject.Models.FileDb context)
    {    
        string filePath = System.Web.HttpContext.Current.Server.MapPath("~/Content/File.txt");

        // read File.txt using filePath and update the database
    }
}

I have the above code in Configuration.cs file in Migrations folder created when entity framework is set up on an ASP .NET MVC project

When I run "Update-Database -Verbose" in Package Manager Console I get an error that filePath is null.

If I manually set filePath with an absolute URL to the file:

string filePath = "C:/Users/User1/My Documents/Visual Studio 2012/Projects/MvcProject/Content/File.txt";

Everything works fine.

Obviously the goal is to have a relative path to enable work with different developers on different setups.

Truth be told all I need is the file - not necessarily the path. Any help will be appreciated.

S.A.
  • 373
  • 1
  • 3
  • 11
  • the filepath is null, or the `System.Web.HttpContext.Current` and you have an exception ? – Aristos Apr 28 '13 at 08:23
  • Aristos, I know filePath is null (or System.Web.HttpContext.Current is). What I don't know is why nor a way to get the filePath in the seed method – S.A. Apr 28 '13 at 08:49
  • If you do not know what is null, then you probably not found how to solve it. I think that you call that not from a page and the `HttpContext` is null and that the issue here. At the end your question must be accurate and not let us imaging the real problems - of course you can follow that only is you seek for solutions. – Aristos Apr 28 '13 at 08:54
  • I posted an answer to your question here: http://stackoverflow.com/questions/19323437/how-to-get-file-path-in-entity-framework-seed-method-without-httpcontext-execut/20070329#20070329 – Wiebe Tijsma Nov 19 '13 at 11:31

2 Answers2

25

I use this function to map paths inside the Seed method, not very clean but it works:

private string MapPath(string seedFile)
{
    if(HttpContext.Current!=null)
        return HostingEnvironment.MapPath(seedFile);

    var absolutePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath; //was AbsolutePath but didn't work with spaces according to comments
    var directoryName = Path.GetDirectoryName(absolutePath);
    var path = Path.Combine(directoryName, ".." + seedFile.TrimStart('~').Replace('/','\\'));

    return path;
}

then just call it using:

   using (var streamReader = new StreamReader(MapPath("~/Data/MyFile.csv")))
Wiebe Tijsma
  • 10,173
  • 5
  • 52
  • 68
0

As I say , I belive that you call it from a page and the System.Web.HttpContext.Current is null, because MapPath function never return null with a not null input - so you get an exception there.

Try that alternative:

string filePath = HttpRuntime.AppDomainAppPath + "/Content/File.txt";

or

string filePath = HostingEnvironment.MapPath("~/Content/File.txt");

Related question: How to access the HttpServerUtility.MapPath method in a Thread or Timer?

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150