If you don't need to do something, the UriType provided by NHibernate will work (I don't know which version has been introduced - I use 3.1.4000). No need to write a custom user type.
Classmap
You can specify UriType in ClassMap<> :
public class ImportedWebImageMap : ClassMap<ImportedWebImage> { public ImportedWebImageMap() { Id(x => x.Id); Map(x => x.SourceUri).CustomType<UriType>(); } }
Property Convention
You can use the hardware agreement to map all Uri properties to use UriType :
public class UriConvention : IPropertyConvention { public void Apply(IPropertyInstance instance) { if (typeof(Uri).IsAssignableFrom(instance.Property.PropertyType)) instance.CustomType<UriType>(); } }
Saving as varchar
If you want to save the Uri in the database as varchar and not the default nvarchar , you can create a non-standard type, which is obtained from UriType and indicates the SQL type AnsiString :
public class UriAnsiStringType : UriType { public UriAnsiStringType() : base(new AnsiStringSqlType()) { } public override string Name { get { return "UriAnsiStringType"; } } }
mattk source share