0

I've followed the instructions on how to wire this up using the docs provided by MS. However, I am not having any success getting the data from the database.

The error I am receiving is this:

Microsoft.Data.Sqlite.SqliteException: SQLite Error 1: 'no such table: ModelRecords'.

I setup the DBContext like so:

public class SsimDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        string db_file = Environment.GetEnvironmentVariable("DocumentUpdaterDbLocation");
        Console.WriteLine(File.Exists(db_file) ? $"Database found at {db_file}" : $"Could not find database at {db_file}");
        optionsBuilder.UseSqlite($"Filename={db_file}");
    }

    public DbSet<ST_Record> ModelRecords { get; set; }
}

The difference here is that I get path to the database from an environmental variable. When I write this variable to the console, it shows the path that I am expecting to the database:

Database found at C:\develop\temp\LFReview\sdb\MasterSdb.sdb

The ST_Record table is defined as:

public class ST_Record
{
    [Key]
    public int ST_RecordID { get; set; }
    public int ProjectID { get; set; }
    public string Name { get; set; }
    public int ID { get; set; }
    public string Description { get; set; }
}

Running this query in the database:

SELECT COUNT(*) FROM ST_Record;

Returns:

COUNT(*)
1808

Finally, the error is thrown here:

List<ST_Record> stt = _context.ModelRecords.Select(r => r).ToList();

Can anybody tell me what I am doing wrong here?

Thanks

Eric
  • 1,182
  • 2
  • 15
  • 25
  • 1
    Your naming is off if your table is named ST_Record. EF is not going to help with that. See https://stackoverflow.com/questions/20184644/howto-specify-table-name-with-entity-framework-code-first-fluent-api – rene Oct 16 '17 at 11:45

2 Answers2

1

Add this line Just below the TargetFramework property of the .csproj file.

<StartWorkingDirectory>$(MSBuildProjectDirectory)</StartWorkingDirectory>

More info: https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=visual-studio

Malith
  • 506
  • 5
  • 9
-1

I meet this problem today and the db file sometimes will not generated in your bin folder , so you will get a error like this Microsoft.Data.Sqlite.SqliteException: SQLite Error 1: 'no such table: you can try to solve this by copying your db file from your project folder to your bin folder.