FluentNHibernate and VARCHAR Columns

I am running a simple .NET project with FluentNhibernate .
I followed a few examples that I found on the Internet, and this seems pretty easy to understand. I realized that if I let FluentNhibernate build my database schema (Sql Server 2000), it generates NVARCHAR fields for the string model properties.

Someone suggested that I could add a type change convention.

This bit of code works fine:

public class AnsiStringConvention : IPropertyConvention { private static Type stringType = typeof(string); public void Apply(FluentNHibernate.Conventions.Instances.IPropertyInstance instance) { if (instance.Property.PropertyType == stringType) { instance.CustomType("AnsiString"); } } } 

Now my DB fields are VARCHAR, as I expected.
I needed to add a component to my class, following the DDD pattern, which I put in an address in a separate class and added it to my Customer class.
I created a separate mapping file for the address:

 public class AddressMap : ComponentMap<Address> { public AddressMap() { Map(x => x.Number); Map(x => x.Street) .Length(100); Map(x => x.City) .Length(50); Map(x => x.PostCode) .Length(5); } } 

Now all the fields of the Customer table are VARCHAR, but the fields of the Address component (Street, City and PostCode) are still created as NVARCHAR.

+6
conventions nvarchar nhibernate fluent-nhibernate
source share
1 answer

Found a solution defining AnsiString as CustomType :

 public class AddressMap : ComponentMap<Address> { public AddressMap() { // this.Map(x => x.Number); this.Map(x => x.Street) .CustomType("AnsiString") .Length(100); this.Map(x => x.City) .CustomType("AnsiString") .Length(30); this.Map(x => x.State) .CustomType("AnsiString") .Length(20); this.Map(x => x.PostalCode) .CustomType("AnsiString") .Length(10); this.Map(x => x.Country) .CustomType("AnsiString") .Length(40); } } 
+11
source share

All Articles