I have a simple data base control handler class.
It uses a SQLiteConnection. I have several tables in my SQLite db.
Now I want to write a generic simple accessor function for all tables of a specific type that use an int Id as primary key. Therefore I have a base class TableWithIntId that always has an int Id as primary key.
My simplified code is:
private SQLiteConnection sqliteConnection;
public T LoadRecord<T>(int id) where T : Database.TableWithIntId
{
try
{
return (from objTable in sqliteConnection.Table<T>()
where objTable.Id == id
select objTable).First();
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
return null;
}
}
The problem is, that I get the following error:
error CS0310: 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'SQLiteConnection.Table()'
This is confusing for me, because T is non abstract and has an automatic default constructor.
public class TableWithIntId
{
[PrimaryKey]
public int Id { get; set; }
}
new()constraint forTinwhereclause.