Trying to migrate the code generation of NHibernate data classes and mappings from 2.1.2 to 3.2 and from Fluent to Mapping ByCode Conformist, I noticed a couple of problems relating mostly to non-Unicode string fields and legacy data types such as image and ntext.
Typically, column mapping uses the Property() method for columns like to NVARCHAR column:
public partial class Application_Map: ClassMapping<Application> { public Application_Map() { Property(x => x.ID, map => { map.Column("ID"); map.NotNullable(true); map.Length(100); }); } }
This kind of declaration raised a HibernateException for VARCHAR columns in configuration.BuildSessionFactory()
Wrong column type in MyDatabase.dbo.Foo for column Bar. Found: varchar, Expected NVARCHAR(256)
To fix this exception, the varchar column needs to be declared like this:
Property(x => x.VarcharFoo, map => { map.Column(c => { c.Name("VarcharFoo"); c.SqlType("varchar"); c.Length(50); }); });
Similarly, we can declare the native SQL data type for image and ntext columns:
Property(x => x.FooImage, map => { map.Column(c => { c.Name("FooImage"); c.SqlType("image"); }); map.Lazy(true); }); Property(x => x.FooNText, map => { map.Column(c => { c.Name("FooNText"); c.SqlType("ntext"); }); });
Pingback: NHibernate OneToMany surprises | devioblog