如何修复EF搭配SpatiaLite时出现的SQLite Error 1模块未找到问题
Looks like you’ve nailed the core issue correctly—this error almost always traces back to SQLite being unable to locate the SpatiaLite extension module, whether you’re using EF Core or raw SQLite connections. Here’s how to resolve it step by step:
1. Ensure the SpatiaLite module is compatible and present
- First, download the correct version of the SpatiaLite module for your system:
- Windows:
mod_spatialite.dll(match 32/64-bit to your project’s target platform) - Linux:
mod_spatialite.so - macOS:
mod_spatialite.dylib
- Windows:
- Stick with pre-compiled bundles from official sources to avoid version mismatches between SpatiaLite and your installed SQLite.
2. Place the module in a SQLite-accessible path
You’ve got a few options here:
- Copy the module file directly to your project’s output directory (e.g.,
bin/Debug/net8.0)—this is the simplest fix for development environments. - Add the module’s directory to your system’s library path (like
PATHon Windows,LD_LIBRARY_PATHon Linux, orDYLD_LIBRARY_PATHon macOS). - Specify the absolute path directly in your code (see the next step for how).
3. Manually set the SpatiaLite path (if auto-loading fails)
If SQLite still can’t find the module automatically, force it by defining the path in your code:
For EF Core:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Filename=TestDb.db", x => x.UseNetTopologySuite()); // Enable direct SpatiaLite loading and set the module path AppContext.SetSwitch("Microsoft.EntityFrameworkCore.Sqlite.UseSpatiaLiteDirectly", true); // Replace with your actual module file path AppContext.SetData("Microsoft.EntityFrameworkCore.Sqlite.SpatiaLitePath", @"C:\YourProject\bin\Debug\net8.0\mod_spatialite.dll"); }
For raw SQLite connections (no EF Core):
using (var connection = new SqliteConnection("Data Source=TestDb.db")) { connection.Open(); using (var command = connection.CreateCommand()) { // Load the extension using its absolute path command.CommandText = "SELECT load_extension('C:\\YourProject\\bin\\Debug\\net8.0\\mod_spatialite.dll')"; command.ExecuteNonQuery(); } }
4. Check for missing dependencies
SpatiaLite relies on additional libraries like proj, geos, and sqlite3 itself. When you download SpatiaLite, make sure to copy all accompanying library files (not just mod_spatialite) to the same directory as your module—missing these will also trigger the "specified module could not be found" error.
5. Verify with SQLite CLI
To rule out project-specific issues, test the module directly with the SQLite command-line tool:
- Open the SQLite CLI and connect to your database:
sqlite3 TestDb.db - Try loading the module:
.load mod_spatialite(or.load /path/to/mod_spatialiteif it’s not in your system path) - If this succeeds, the problem lies in your project’s path configuration; if it fails, double-check your module file and dependencies.
内容的提问来源于stack exchange,提问作者Jarb

