1

Hiyya,

We're currently working on a project where we need to locally store data (configuration data, more specifically) and figured Sqlite is a decent solution for now.

However, after setting up the DBContext, Entities and whatnot, creating a migration and running Update-Database to create the database (and confirming through DB Browser for SQLite that it "properly" created it), we are met with a "No such table found" error.

DataService.cs

private SettingsModel GetConfig()
    {
        using(var _context = new DataContext())
        {
            var query = (from a in _context.SettingsModel
                         select a);

            var obj = query.FirstOrDefault();
            if (string.IsNullOrWhiteSpace(obj.ExpressionString))
                obj.ExpressionString = DEFAULT_EXPRESSION_STRING;

            return obj;
        }
    }

DbContext.cs

public class DataContext : DbContext
{
    public virtual DbSet<SettingsModel> SettingsModel { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlite("Data source=keyconfig.db");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SettingsModel>(entity =>
        {
            entity.HasNoKey();
            entity.Property(e => e.ExpressionString)
                .IsUnicode(false);
        });
    }
}

SettingsModel.cs

public class SettingsModel
{
    public string ExpressionString { get; set; }
}

I am honestly quite dumb-founded why this is not working (or why we're having the issue we're having), especially after confirming the database and it's table(s) does exist, so any and all help would be appreciated!

Xariez
  • 759
  • 7
  • 24
  • 1
    Are you sure the database is created where the running application looks for it? – Gert Arnold Apr 02 '20 at 13:58
  • Examine `_context.Database.GetDbConnection().ConnectionString`, does it point to the existing database containing the table? – Ivan Stoev Apr 02 '20 at 13:59
  • @IvanStoev Thank you for bringing attention to that! Apparently it's not happy over the fact that the table has no primary key, which also makes sense. Thanks! – Xariez Apr 02 '20 at 15:14
  • @IvanStoev Actually, it would seem I was a bit too optimistic. The error remains. As for the connection string, that is simply what you see in the snippet above (`optionsBuilder.UseSqlite(...)`). Perhaps some kind of absolute bath is required? – Xariez Apr 03 '20 at 02:56
  • @IvanStoev Okay, much like I suspected it would seem we need a absolute path given (for now) – Xariez Apr 03 '20 at 10:25
  • @Xariez Eventually you can try utilizing `|DataDirectory|` as described in the [docs](https://learn.microsoft.com/en-us/dotnet/standard/data/sqlite/connection-strings#data-source), although it's not quite clear how it works - see https://stackoverflow.com/a/12276625/5202563. Good luck! – Ivan Stoev Apr 03 '20 at 10:57
  • Thanks again! If you post something as an actual "answer", I'll mark it as such as well! :) @IvanStoev – Xariez Apr 03 '20 at 12:13

1 Answers1

0

Temporary fix was to use an absolute path to the .SQLITE database, instead of a relative one. Will be looked into/troubleshooted again later.

Xariez
  • 759
  • 7
  • 24