Let's imagine that you have .NET Core application and your appsettings.json
file looks like this:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Production": {
"SqliteConnectionString": "Filename=./MyDatabase.sqlite"
}
}
You can get SqliteConnectionString
value from Startup.cs
like this:
public void ConfigureServices(IServiceCollection services)
{
var connection = Configuration["Production:SqliteConnectionString"];
services.AddDbContext<MyContext>(options =>
options.UseSqlite(connection)
);
....
}
And then in your DBContext
you should add constructor that accepts DbContextOptions
:
public class MyContext : DbContext
{
public MyContext (DbContextOptions<MyContext> options) : base(options)
{ }
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…