【问题标题】:ServiceStack.OrmLite Create table with table nameServiceStack.OrmLite 用表名创建表
【发布时间】:2015-06-24 01:31:21
【问题描述】:

我正在使用 ServiceStack.OrmLite 版本 3.9.71,我想创建具有特定表名的表。所以我想要类似下面的东西

db.CreateTable<TableType>("Table Name");

我找到了以下相关问题,但看起来它使用的是较新版本。

Create table with custom name dynamically and insert with custom table name

【问题讨论】:

    标签: sql-server servicestack-bsd


    【解决方案1】:

    我已经想出了如何做到这一点。 v3 有一个名为 GetModelDefinition 的方法,它的作用与 GetModelMetaData 相同,所以我修改了答案 Create table with custom name dynamically and insert with custom table name 中的类。

    以下是我使用的最后一个类

    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; });
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多