I am trying to create a few helper methods in a base generic class and OrmLite is issuing queries for tables using the base class’s name instead of … I’ve tried both abstract and partial class types without any luck. Any suggestions?
public partial class BaseRecordOrmLite<T> where T : new()
{
....
public int Save()
{
using (var Db=GetConnection())
{
try
{
Db.Insert(this);
return 1;
}
catch(Exception e)
{
var s = Db.GetLastSql(); //INSERT INTO `BaseRecordOrmLite`1` (`id`,`....
return -1;
}
}
}
OrmLite always needs the concrete Type in order to be able to use the metadata for the concrete table, so you can’t use the base class type.
Also Are you embedding data logic in your data models? I’d definitely recommend against doing that, they ideally should be logic-less POCOs so they can be also be reused and serialized if needed. Extension methods are a great way of adding reusable functionality without adding implementation coupling inside the data model.
When using generics you need the generic argument to contain the concrete type, so you should be able to do something like:
public static int SaveRecord<T>(IDbConnecion db, T record) where T : BaseRecordOrmLite
{
db.Insert(record);
return 1;
}
But in this case since you’re just inserting the record you don’t need the generic constraint:
public static int SaveRecord<T>(IDbConnecion db, T record)
{
db.Insert(record);
return 1;
}
which you can then save with:
using (var db = GetConnection())
{
db.SaveRecord(entity);
}
thank you that will work as I can call the static save from below it. I have been able to serialize (json) from inside the base before I guess more metadata is needed at runtime for the relational side of things? Doesn’t matter…this works. thanks again