The problem involves using Entity Framework Core with SQLite in a .NET Console application where despite applying migrations, the ‘Members’ table** does not seem to exist in the SQLite database.** This issue typically manifests as a Microsoft.Data.Sqlite.SqliteException with the message no such table: Members when attempting to query or access data from the Members table in the database.
I’m setting up a .NET Console application using Entity Framework Core with SQLite to manage member data. I configured EF Core with a connection string (“DefaultConnectionString”: “DataSource=members.db;”) in appsettings.json and defined a Member class for the Members table structure. My MemberContext class extends DbContext with a DbSet for data access.
After applying migrations (dotnet ef migrations add and dotnet ef database update), indicating successful schema creation, I attempted to query the Members table in Program.cs. However, I encountered a Microsoft.Data.Sqlite.SqliteException stating “no such table: Members”. This suggests the table isn’t found despite the migration process.
I’m now troubleshooting why the ‘Members’ table isn’t created in SQLite as expected post-migration. Any insights would be appreciated.
Here is my Code:
My Program.cs file:
using System.Text;
using System.Xml.Serialization;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Uebung;
using Uebung.Data.Models;
await using var db = new MemberContext();
Console.WriteLine($"Database path: {db.MemberDb}");
Console.WriteLine("Querying all members:");
var results =
from member in db.Members
select member;
await foreach (var s in results.AsAsyncEnumerable())
{
Console.WriteLine("Member: " + s.Vorname + " " + s.Nachname);
}
My appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings":
{
"DefaultConnectionString": "DataSource=members.db;"
}
}
My Member.cs file:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
namespace Uebung.Data.Models;
public class MemberContext : DbContext
{
public DbSet<Member> Members { get; set; }
public string MemberDb { get; set; }
public MemberContext()
{
MemberDb = "members.db";
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Data Source={MemberDb}");
}
}
public class Member
{
[Key]
public int ID { get; set; }
public string? Vorname { get; set; }
public string? Nachname { get; set; }
}
And my AppDbContext.cs File:
using Microsoft.EntityFrameworkCore;
using Uebung;
public class AppDbContext : DbContext
{
public DbSet<Uebung.Data.Models.Member> Members { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
}
Görkem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.