Search code examples
sqliteservicestackormlite-servicestack

Tables with schema using SqliteDialect.Provider


In my test project I register a connection using ":memory" connection string and SqliteDialect.Provider as provider. When trying to run tests that execute arbitrary sql (I have a complex join statement so I cannot use a typed query) I get an error that the table do not exist. In my query I print "SELECT * FROM xxx.Table1" but when looking at the code generated from a typed query I can see that it is "SELECT * FROM xxx_Table1". I need to use schemas for the production code, so is there a way to force ORMLite for Sqlite to generate schemas and not just name prefixes when using the attribute [Schema("xxx")] on my domain models?


Solution

  • SQLite doesn't have schemas so they're simulated by prefixing the schema name before the table name. However this should be a transparent implementation detail for SQLite :memory: DBs as the same table name will be used when creating or querying the table.

    If you're creating Custom SQL you should use the tableName returned from:

    var modelDef = typeof(Table1).GetModelMetadata();
    var tableName = db.GetDialectProvider().GetTableName(modelDef);
    var sql = $"SELECT * FROM {tableName}";