我正在使用 ServiceStack.OrmLite 版本 3.9.71,我想创建具有特定表名的表。所以我想要类似下面的东西
db.CreateTable<TableType>("Table Name");
我发现了以下相关问题,但看起来它使用的是较新版本。
我正在使用 ServiceStack.OrmLite 版本 3.9.71,我想创建具有特定表名的表。所以我想要类似下面的东西
db.CreateTable<TableType>("Table Name");
我发现了以下相关问题,但看起来它使用的是较新版本。
我已经想出了如何做到这一点。v3 具有名为 GetModelDefinition 的方法,该方法与 GetModelMetaData 执行相同的操作,因此我修改了答案Create table with custom name dynamic 中的类并使用自定义表名插入。
以下是我正在使用的最后一个类
public static class GenericTableExtensions
{
static object ExecWithAlias<T>(string table, Func<object> fn)
{
var modelDef = SqlServerOrmLiteDialectProvider.GetModelDefinition(typeof(T));
lock (modelDef)
{
var hold = modelDef.Alias;
try
{
modelDef.Alias = table;
return fn();
}
finally
{
modelDef.Alias = hold;
}
}
}
public static void CreateTable<T>(this IDbConnection db, string table) where T: new()
{
ExecWithAlias<T>(table, () => { db.CreateTable<T>(); return null; });
}
}