2

In my entity framework Seed method I have the following line to get a file from a different project:

 var filePath = new DirectoryInfo(HostingEnvironment.ApplicationPhysicalPath).Parent.FullName + "\\Com.ProjectX\\companies.xls";

This works when a HttpContext is available, like when using this action method to trigger it:

public ActionResult About()
    {
        var configuration = new Com.EntityModel.Configuration();
        var migrator = new System.Data.Entity.Migrations.DbMigrator(configuration);
        migrator.Update();

        return View();
    }

However, it doesn't work when I execute Update-Database from the package manager console (the file isn't found and it's hard to debug because I also can't breakpoint when doing that.

I'd like to be able to use Update-Database command and have it work without an HttpContext. How can I get the path?

parliament
  • 21,544
  • 38
  • 148
  • 238
  • I'm not sure what you're getting at.. Are you saying that you're unable to get the `filePath`? Shouldn't you use `HostingEnvironment.MapPath` instead? – Nate-Wilkins Oct 11 '13 at 16:58
  • The problem is HostingEnvironment.ApplicationPhysicalPath == null if not in the context of an Http Request. How can I used HostingEnvironment.MapPath to get a path in a different project without relative paths? MapPath("~/") goes to the root of the current project I need to go to a different project without relatve paths which is why I used HostingEnvironment.ApplicationPhysicalPath and traveled up to the Parent – parliament Oct 11 '13 at 17:12
  • possible duplicate of http://stackoverflow.com/questions/16260975/entity-framework-how-to-get-relative-file-path-in-seed-method – Wiebe Tijsma Nov 19 '13 at 11:30

2 Answers2

5

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).AbsolutePath;
    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")))

Additionally, to debug the Seed method I like to just throw an Exception with the message I want to display (I'm a beginner in EF, I haven't figured out how to write to the NuGet Package manager console yet :))

Wiebe Tijsma
  • 10,173
  • 5
  • 52
  • 68
0
private string GetPath(string relativeFilePath)
{
    var absolutePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath;
    var directoryName = Path.GetDirectoryName(absolutePath);
    var path = Path.Combine(directoryName, ".." + relativeFilePath.TrimStart('~').Replace('/', '\\'));

    return System.Web.HttpUtility.UrlDecode(path); // decode spaces in path :(
}
Denis Bubnov
  • 2,619
  • 5
  • 30
  • 54
ronald
  • 11
  • 2