Code-First SQLite初始化后表未创建,连接字符串修改问题排查
Alright, let's dig into why your SQLite database tables aren't showing up after adjusting the connection string in your myDbContext. Here are the most common issues to check:
1. You're modifying the connection string too late
EF (whether it's EF6 with System.Data.SQLite or EF Core) initializes its database context based on the connection string early in the context's lifecycle. When you set base.Database.Connection.ConnectionString after the context has already been initialized, EF has already locked in the original connection string from your config file and won't reprocess the new one for database creation.
Instead, set the connection string before the context finishes initializing. The best places to do this are:
- EF Core: Override the
OnConfiguringmethod:public class MyDbContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { string dbPath = Path.Combine(AppVariable.myPath, "database", "data.db"); string connectionString = $"data source={dbPath};"; optionsBuilder.UseSqlite(connectionString); } } } - EF6: Pass the connection string directly in the constructor:
Thepublic class MyDbContext : DbContext { public MyDbContext() : base(new SQLiteConnection(GetConnectionString()), true) { } private static string GetConnectionString() { string dbPath = Path.Combine(AppVariable.myPath, "database", "data.db"); return $"data source={dbPath};"; } }trueparameter tells EF that the context should manage the connection's lifecycle.
2. You're not triggering database creation explicitly
EF won't automatically create tables unless you tell it to. Even with the correct connection string, you need to run a method to initialize the database:
- EF Core: Call
EnsureCreated()when your app starts (e.g., inProgram.cs):using (var context = new MyDbContext()) { context.Database.EnsureCreated(); } - EF6: Use
CreateIfNotExists()orInitialize(true):
If you're using migrations, make sure you've applied them against the correct connection string too.using (var context = new MyDbContext()) { context.Database.CreateIfNotExists(); // Or context.Database.Initialize(true); }
3. Your path is malformed (common gotcha!)
Manual string concatenation for paths is prone to errors. For example, if AppVariable.myPath doesn't end with a slash, you'll end up with a broken path like C:\MyAppdatabase\data.db instead of C:\MyApp\database\data.db.
Always use Path.Combine to safely build paths:
string dbPath = Path.Combine(AppVariable.myPath, "database", "data.db"); string connectionString = $"data source={dbPath};";
Also, double-check that your app has read/write permissions for the target directory—if it can't write to the path, it won't create the database file.
4. EF isn't aware of your entity classes
If your MyDbContext doesn't include DbSet<T> properties for your entities, or you haven't configured them in OnModelCreating, EF won't generate tables for them. Make sure you've added all your entities:
public class MyDbContext : DbContext { public DbSet<Customer> Customers { get; set; } public DbSet<Order> Orders { get; set; } // Add other DbSets for your entities here }
Start by verifying the connection string timing and explicit database creation call—those are the most likely culprits. Then check your path and entity configurations if those don't fix it.
内容的提问来源于stack exchange,提问作者yasmin shahidi

