I created a ASP.NET MVC website using .NET Core 2.2 using a SQLite database. So far it's working well. Trouble begins when I want to add SQLite-specific keywords to the connection string, such as
Data Source=~\\App_Data\\MyDb.db; Version=3; DateTimeFormat=UnixEpoch; DateTimeKind=Utc
Now I get
Keyword not supported: 'version'
I register the database like this
// ConfigureServices(IServiceCollection services)
var conn = Configuration.GetConnectionString("MyDB").Replace("~", _env.ContentRootPath);
services.AddDbContext<MyDBContext>(options => options.UseSqlite(conn));
Then MyDBContext has
public partial class MyDBContext : DbContext
{
public MyDBContext() { }
public SatrimonoContext(DbContextOptions<MyDBContext> options)
: base(options) { }
public virtual DbSet<Book> Book { get; set; }
}
Then I use it in my page Model
private SatrimonoContext _db;
public BookAccuracyListModel(SatrimonoContext dbContext)
{
_db = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
and from there I can access _db normally via LINQ.
Before posting here, I did plenty of research on the topic, and the best responses I found were this
This provider is Microsoft.Data.Sqlite. Those connection strings are for System.Data.SQLite.
We support the following keywords: Cache, Data Source, Mode.
and this
The issue I was having was because I was trying to create a SqlConnection instead of a SQLiteConnection. Making that change solved my issue.
However, if I'm doing it "right", I'm not creating the SqlConnection and thus can't change it to SQLiteConnection. The other response doesn't include a solution.
So how do I get this to work the right way?
Microsoft.Data.Sqlite
, and that will probably mean that you cannot use EF Core