如何告诉FNH将snake-case数据库列映射到Pascal案例属性?

时间:2013-02-07 19:23:34

标签: fluent-nhibernate-mapping

我们有惯例将snake-case db列转换为我们实体中的pascal case:

        Map(x => x.ProjectName).Column("project_name");

映射总是这样。我们怎样才能告诉FNH做这项工作而不是全部输入?

1 个答案:

答案 0 :(得分:0)

实施IPropertyConvention并使用

// in Apply (maybe filter out some special cases)
instance.Column(ToSnakeCase(instance.Name));


private string ToSnakeCase(string name)
{
    var result = new Stringbuilder(name.Length);
    for (int i = 0; i < name.Length; i++)
    {
        if (i > 0 && char.IsUpper(name[i]))
            result.Append('_').Append(char.ToLower(name[i]));
        else
            result.Append(char.ToLower(name[i]));
    }
    return result.ToString();
}