0

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?

Isaac Bolinger
  • 7,328
  • 11
  • 52
  • 90
  • I don't really need an answer to this anymore, I just went back to using int for identifiers for most things now. I'd use Guid if MariaDB had better support for it. – Isaac Bolinger Nov 07 '14 at 19:33

1 Answers1

1

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

cbp
  • 25,252
  • 29
  • 125
  • 205