How to set the right AttachDbFilename relative path in ASP.NET Core?

前端 未结 2 1642
臣服心动
臣服心动 2020-12-06 07:22

Working in VS2015 on a web project based on ASP.NET Core (former ASP.NET 5), .NET Core CLR RC1, EF Core (former EF 7), EF Migrations enabled, LocalDb v11.0.

I manual

相关标签:
2条回答
  • 2020-12-06 08:02

    I found an easier way around

    1. run your app and create the DB in the default location

    2. Open Microsoft Sql Server Management Studio (or you prefer IDE) and create a new connection to point to (localdb)\mssqllocaldb

    3. Script Database as CREATE

    4. Change the path in the FILENAME

    5. Remove the DB you created on step 1 (choose close existing connections)

    6. Run the script

    Your application should work without changing anything in the config

    0 讨论(0)
  • 2020-12-06 08:15

    I do now have this working and it was hard work. The key to this is the environment information "ContentRootPath" which in your example will return the path for MySolution\src\MyProject

    My test app is the "Database First" tutorial following https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html

    with changes including this one to suit my situation of teaching web app programming and needing self contained apps that I and students can run on each other's machines for discussion, marking etc.

    In appsettings.json

        {
      "ConnectionStrings": {
        "DefaultConnection": "Server=(localdb)\\mssqllocaldb;AttachDBFilename=%CONTENTROOTPATH%\\App_Data\\blogging.mdf;Trusted_Connection=true;MultipleActiveResultSets=true"
      }
    

    With the distinctive part being:

    AttachDBFilename=%CONTENTROOTPATH%\\App_Data\\blogging.mdf
    

    OK I am using the traditional name "App_Data" but it is more securely under the ContentRootPath rather than under "wwwroot".

    Then in Startup.cs

    public class Startup
    {
        //20160718 JPC enable portable dev database
        private string _contentRootPath = "";
    
        public Startup(IHostingEnvironment env)
        {
            //20160718 JPC enable portable dev database
            _contentRootPath = env.ContentRootPath;
        ...
        }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //20160718 JPC enable portable dev database
            string conn = Configuration.GetConnectionString("DefaultConnection");
            if(conn.Contains("%CONTENTROOTPATH%"))
            {
                conn = conn.Replace("%CONTENTROOTPATH%", _contentRootPath);
            }
            ...
         }
    

    In the above "..." represents the standard code generated by Visual Studio 2015.

    NOTE that when we "Publish" an app like this, we need to manually copy and paste custom folders and files, eg my "App_Data" folder, into the published version. OR we can add the custom folder name, in this case "App_Data", to the file "project.json".

    It is also good to know that for any class including controller classes, we can add a constructor method with parameter env and the hosting environment will feed us useful information including ContentRootPath. Useful for custom file storage eg providing file upload for our users.

    public class HomeController : Controller
    {
        //20160719 JPC access hosting environment via controller constructors
        private IHostingEnvironment _env;
    
        public HomeController(IHostingEnvironment env)
        {
            _env = env;
        }
    
        public IActionResult Index()
        {
            string contentRootPath = _env.ContentRootPath;
            return View();
        }
    

    OK this is only to demo the principle as in I add a breakpoint on "return View()" then hover the mouse over contentRootPath to make the point.

    ASP.NET Core MVC6 looks like one of the bigger learning and teaching challenges I have run into. Good luck with it for all of us. I have found one nice advance: In MVC5 we had some drama getting our custom data and the identity AspNetUser tables to live together nicely in one database. Looks like it is working out as a more neat and tidy proposition in MVC6.

    0 讨论(0)
提交回复
热议问题