我可以告诉nhibernate完全将System.Data.DbType换成自定义IUserType吗?

时间:2014-11-07 00:55:27

标签: nhibernate nhibernate-4

我看到如何使用映射文件为我想要的任何nhibernate映射类指定自定义IUserType。

但是,我不想每次都输入它。有没有办法覆盖这里看到的标准映射表?enter image description here

我有JørnSchou-Rode的IUserType实现,用于在MariaDB中的Binary(16)中存储guid。我想要的只是输入一行或两行代码来告诉Nhibernate,当它看到一个System.Guid将它转换为Schou-Rode给我的自定义“BinaryGuidType”时。可以吗?

1 个答案:

答案 0 :(得分:1)

如果您使用Fluent NHibernate,您可以使用约定轻松完成此操作。以下是我将所有字符串映射到varchar而不是nvarchar:

的方法
public class PropertyConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        SetStringsAsAnsiStringByDefault(instance);
    }

    private void SetStringsAsAnsiStringByDefault(IPropertyInstance instance)
    {
        if (instance.Property.PropertyType == typeof(string))
        {
            instance.CustomType("AnsiString");
        }
        else if (instance.Property.PropertyType == typeof(char))
        {
            instance.CustomType("AnsiChar");
        }
    }
}

我相信NHibernate的更高版本具有内置的约定支持,但文档似乎很少。以下是您开始使用的文章:http://weblogs.asp.net/ricardoperes/nhibernate-conventions

相关问题