Can I tell nhibernate to swap a System.Data.DbType out for a custom IUserType completely?

174 Views Asked by At

I see how I can use the mapping file to specify a custom IUserType for any nhibernate mapped class I want.

However, I don't want to type it in every time. Is there a way to override the standard mapping table seen here?enter image description here

I have Jørn Schou-Rode's IUserType implementation for storing a guid in Binary(16) in MariaDB. All I want is to enter one or two lines of code to tell Nhibernate when it sees a System.Guid to convert it to the custom "BinaryGuidType" that Schou-Rode made me. Can it be done?

1

There are 1 best solutions below

1
On

If you are using Fluent NHibernate you can easily do this using Conventions. Here is how I map all strings to varchar instead of 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");
        }
    }
}

I believe the later versions of NHibernate have in-built support for conventions, but the documentation seems to be sparse. Here is an article for you to get started though: http://weblogs.asp.net/ricardoperes/nhibernate-conventions