-
Notifications
You must be signed in to change notification settings - Fork 97
Relative path in Connection string pointing to different locations #275
Comments
A solution to this issue is complicated by what may be another issue. The connection string setting in Startup.cs is ignored
Instead the effective string setting seems to come from the OnConfiguring() method in the DbContext class. This requires some setup in the Startup.cs class so that IConfiguration and IHostingEnvironment can be injected into the DbContext constructor. In addition to the ConfigureServices method shown above I have a Startup method as shown with 2 additional properties
The properties Configuration and Environment are set as Singletons for Dependency Injection into the constructor for the DbContext in use
Now the Environment can read the ContentRootPath and combine that with the Connection string from the appsettings.json file
It will now detect the correct ContentRootPath for the executable environment making the other connections strings redundant. This appears to be a functional workaround for the issue. |
@jwdavidson It appears that this issue is also affecting my code. I'm currently working on replicating your workaround to see if it solves my problem. |
This is likely a bug in Entity Framework, not in Microsoft.Data.Sqlite. To help us debug, we need more info. You can add the verbose option More info: If this datadirectory is not specified, Microsoft.Data.Sqlite uses the application base directory as the relative path's root. In .NET Core, this folder is See also https://github.com/aspnet/Microsoft.Data.Sqlite/wiki/Connection-Strings for full explanation of relative file paths. See https://github.com/aspnet/Microsoft.Data.Sqlite/blob/1.0.0/src/Microsoft.Data.Sqlite/SqliteConnection.cs#L208-L217 for its implementation. |
It appears the options.UseSqlite(ConnectionString) method ignores the connection string passed to it. I had to configure the DbContext in it's OnConfiguring method, in order for it to work. However, I was not able to replicate the pathing issues. |
@jwdavidson I wasn't able to reproduce the error either, but it's definitely possible that runtime and design-time could be computing two different paths. To help us debug this, add the following lines just inside your startup constructor. Then share the output when you run EF commands vs launching your ASP.NET app. public class Startup
{
public Startup(IHostingEnvironment env)
{
Console.WriteLine("Content root path = " + env.ContentRootPath);
Console.WriteLine("App base path = " + Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default.Application.ApplicationBasePath);
Console.WriteLine("Current dir = " + System.IO.Directory.GetCurrentDirectory()); |
I will be able to get some time Thursday morning and will do it then. If I get an opportunity to do it earlier I will thanks for all your work Sent from my iPad
|
Finally got some time to run a test with the output requested:
|
@jwdavidson I'm not sure why As I can't reproduce and there is a workaround, I'm closing the investigation for now. Feel free to continue discussion. We can reopen if we find this problem is reported by others and/or if we can reproduce it. FYI the tooling has been completely refactored recently (dotnet/efcore#5334). I'll keep an eye out for this behavior as we finish testing this new version of the tooling. |
@jwdavidson we received another report that helped us find the same issue you experienced. It's a problem with ASP.NET Web Tooling in VS. I opened dotnet/efcore#6335 to track this. |
It looks like the changes in aspnet/Hosting #633 (announcement #157) undid the work to resolve aspnet/Microsoft.Data.Sqlite #188.
Using aspnet core 1.0.0 the update-database command creates the database and correctly populates the schema at
<appName>/src/<appName>/bin/src/<appName>/bin/Debug/netcoreapp1.0/<dbname>.db
When the MVC app runs it creates an empty database at
<appName>/src/<appName>/bin/Debug/netcoreapp1.0/<dbname>.db
The connection string from appsettings.json
The updated startup.cs (after #633)
WebRootPath and ContentRootPath do not appear to be available in the ConfigureServices method, nor is there a GetBasePath extension available to mirror the SetBasePath extension.
Is there an approved method to set a relative path for an sqlite database so that an MVC application can be made to work in Windows, Linux and macOS?
The text was updated successfully, but these errors were encountered: