Efcore: Microsoft.Data.Sqlite: SQLite provider seems to ignore |DataDirectory|

Created on 30 Jul 2018  路  3Comments  路  Source: dotnet/efcore

Moved from https://github.com/aspnet/EntityFrameworkCore/issues/12767 posted by @bdongus

Unlike the SQLserver provider the SQLite provider seems to fail at resolving the placeholder "|DataDirectory|".

Microsoft.Data.Sqlite.SqliteException
  HResult=0x80004005
  Nachricht = SQLite Error 14: 'unable to open database file'.
  Quelle = Microsoft.Data.Sqlite
  Stapel眉berwachung:
   bei Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   bei Microsoft.Data.Sqlite.SqliteConnection.Open()
   bei Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   bei Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   bei Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.Open(Boolean errorsExpected)
   bei Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Create()
   bei Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()
   bei Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
   bei idee5.Globalization.Test.WithSQLiteBase.MyTestInitalize() in C:\Users\xyz\Source\idee5 intern\idee5.Globalization4netcore\idee5.Globalization.Test\WithSQLiteBase.cs: Zeile23

Steps to reproduce

c# var contextOptions = new DbContextOptionsBuilder<GlobalizationDbContext>(); contextOptions.UseSqlite("data source=|DataDirectory|idee5.Resources.db3"); context = new GlobalizationDbContext(contextOptions.Options); context.Database.EnsureDeleted(); context.Database.EnsureCreated();
The exception occurs in EnsureCreated.

Further technical details

EF Core version: 2.1.1
Database Provider: Microsoft.EntityFrameworkCore.Sqlite 2.1.1
Operating system: Windows 10
IDE: Visual Studio 2017 15.7.5

area-adonet-sqlite closed-fixed good first issue help wanted type-enhancement

Most helpful comment

@bricelam @ajcvickers Raised a PR for supporting this. Let me know what you think.

All 3 comments

From @bricelam:

Microsoft.Data.Sqlite just passes the filename directly to the native SQLite engine--it doesn't process the |DataDirectory| replacement string. You can do it yourself:

var builder = new SqliteConnectionStringBuilder(connectionString);
builder.DataSource = builder.DataSource
    .Replace(
        "|DataDirectory|",
        AppDomain.CurrentDomain.GetData("DataDirectory") as string);
connectionString = builder.ToString();

From @bdongus:

@bricelam Thank you for your quick and helpful answer. Currently I do something similar. As it should be no big deal to do it in UseSQLite itself, I hope the team considers including it in the next releaase. That way noone needs to do it on his own or write a wrapper if used frequently.

@bricelam @ajcvickers Raised a PR for supporting this. Let me know what you think.

Was this page helpful?
0 / 5 - 0 ratings