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 MySolutionsrcMyProject
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.