efcore: In EF7, SqLite unable to load spatialite using the NetTopologySuite Package
Hi,
In EF 7 I using the Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite package a simple example does not seem to be able to load the spatialite library
I get “Microsoft.Data.Sqlite.SqliteException: ‘SQLite Error 1: ‘’.’” implying sqlite has returned an error code, but not given any more information about it.
This was all working fine in EF 6
Stacktrace:-
Microsoft.Data.Sqlite.dll!Microsoft.Data.Sqlite.SqliteConnection.LoadExtensionCore(string file, string proc) Unknown
Microsoft.Data.Sqlite.dll!Microsoft.Data.Sqlite.SqliteConnection.LoadExtension(string file, string proc) Unknown
Microsoft.EntityFrameworkCore.Sqlite.dll!Microsoft.EntityFrameworkCore.Infrastructure.SpatialiteLoader.Load(System.Data.Common.DbConnection connection) Unknown
Microsoft.EntityFrameworkCore.Sqlite.dll!Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.InitializeDbConnection(System.Data.Common.DbConnection connection) Unknown
Microsoft.EntityFrameworkCore.Sqlite.dll!Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.SqliteRelationalConnection(Microsoft.EntityFrameworkCore.Storage.RelationalConnectionDependencies dependencies, Microsoft.EntityFrameworkCore.Storage.IRawSqlCommandBuilder rawSqlCommandBuilder, Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger<Microsoft.EntityFrameworkCore.DbLoggerCategory.Infrastructure> logger) Unknown
Example code:-
var sqliteConnection = new SqliteConnection($"DataSource=c:\\temp\\testdb.db");
var _connection = sqliteConnection;
_connection.Open();
var optionsBuilder = new DbContextOptionsBuilder<DbContext>();
optionsBuilder.UseSqlite(_connection, arg => arg.UseNetTopologySuite());
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 5
- Comments: 18 (7 by maintainers)
Commits related to this issue
- Update to SQLitePCLRaw 2.1.4 Fixes #29584 — committed to bricelam/efcore by bricelam a year ago
- [release/7.0] Update to SQLitePCLRaw 2.1.4 (#30036) Fixes https://github.com/dotnet/efcore/issues/29584 — committed to dotnet/efcore by bricelam a year ago
I faced the same issue and came up with the following work-a-round, which is working for me (.net 7 / EF Core 7 / Windows):
In my database context in the OnConfiguring call:
I use a IDbContextFactory to create / load a new database context. I have p/invoke definition inside the class:
In the the CreateDbContext() call I have:
Probably this can be simplified, but I noticed the p/invoke call to spatialite_version triggers the load of the module in mem, and the dynamic load of the mod_spatialite module did the trick (see https://www.gaia-gis.it/fossil/libspatialite/wiki?name=mod_spatialite).
EF Core internally use the sqlite function call to load an extension that seem to fail without any (reasonable) reason.
btw; same PackageReferences as @woodmeister
Thank you for the good work!
Op ma 9 jan. 2023 19:56 schreef Brice Lambson @.***>:
Workaround good for me too.
I needed to put the sqlitecommand in a using so it get’s disposed, otherwise there are problems with the machinery later on.