Search code examples
postgresqlormlite-servicestack

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. I have created my POCO class as shown:

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. 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. 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

This makes sense, because my database has a column named companyname, not company_name in the company table. PostgreSQL is particular about case, so I made my column names all lower case. However, my POCO properties are camel case. It looks like the PostgreSQL provider code is forcing camelcase properties to be named with an _ symbol in between each capital letter. 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.

Can I change this behavior so that my POCO properties map to lowercase without the _ symbol?

Thanks


Solution

  • 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();