简体   繁体   English

我可以让OrmLite为PostgreSQL列名使用小写字母,而不是使用带下划线命名的小写字母吗?

[英]Can I have OrmLite use lowercase for PostgreSQL column names rather than the provided lowercase with underbar naming?

I am looking into ServiceStack and am using OrmLite against a PostgreSQL database. 我正在调查ServiceStack,并针对PostgreSQL数据库使用OrmLite。 I have created my POCO class as shown: 我创建了POCO类,如下所示:

public class Company
{
    [AutoIncrement]
    public long Id { get; set; }
    public string CompanyName { get; set; }
    public int NumberOfLicenses { get; set; }
}

I also setup the database connection in the Global.asax file as per the directions on the SS site. 我还按照SS网站上的说明在Global.asax文件中设置了数据库连接。 Here is that code: 这是该代码:

container.Register<IDbConnectionFactory>(
                c => new OrmLiteConnectionFactory("Server=localhost;Port=5432;SearchPath=company;Database=company;User Id=ABC; Password=XYZ;", PostgreSqlDialect.Provider)
                {
                    ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
                });

As you can see, I have the PostgreSQL dialect provider and mini-profiler in play. 如您所见,我正在使用PostgreSQL方言提供程序和mini-profiler。 Since I am testing, I am just adding fake data here as well: 由于我正在测试,因此我也只是在此处添加虚假数据:

using (IDbConnection db = container.Resolve<IDbConnectionFactory>().Open())
            {
                db.Insert(new Company { CompanyName = "Company A", NumberOfLicenses = 13});
                db.Insert(new Company { CompanyName = "Company B", NumberOfLicenses = 82});
                db.Insert(new Company { CompanyName = "Company C", NumberOfLicenses = 16});
                db.Insert(new Company { CompanyName = "Company D", NumberOfLicenses = 8});
                db.Insert(new Company { CompanyName = "Company E", NumberOfLicenses = 107});
                db.Insert(new Company { CompanyName = "Company F", NumberOfLicenses = 56});
            }

When I run the application, I get the error: ERROR: 42703: column "company_name" of relation "company" does not exist 运行应用程序时,出现错误:错误:42703:关系“公司”的列“ company_name”不存在

This makes sense, because my database has a column named companyname, not company_name in the company table. 这是有道理的,因为我的数据库在company表中有一个名为companyname的列,而不是company_name。 PostgreSQL is particular about case, so I made my column names all lower case. PostgreSQL特别强调大小写,因此我将列名全部小写。 However, my POCO properties are camel case. 但是,我的POCO属性是骆驼的情况。 It looks like the PostgreSQL provider code is forcing camelcase properties to be named with an _ symbol in between each capital letter. 似乎PostgreSQL提供程序代码强制在每个大写字母之间用_符号命名驼峰属性。 In the database I temporarily renamed the companyname column to company_name, and sure enough, the error moved to the NumberOfLicenses property, which became number_of_licenses. 在数据库中,我暂时将companyname列重命名为company_name,并且确实将错误转移到NumberOfLicenses属性,该属性变为number_of_licenses。

Can I change this behavior so that my POCO properties map to lowercase without the _ symbol? 我可以更改此行为,以便我的POCO属性映射为小写而不带_符号吗?

Thanks 谢谢

You can use specify your own naming strategy with: 您可以使用以下命令指定自己的命名策略:

public class LowercaseNamingStrategy : OrmLiteNamingStrategyBase
{
    public override string GetTableName(string name)
    {
        return name.ToLower();
    }

    public override string GetColumnName(string name)
    {
        return name.ToLower();
    }

}

OrmLiteConfig.DialectProvider.NamingStrategy = new LowercaseNamingStrategy();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM