FluentNHibernate和VARCHAR列
我正在用FluentNhibernate启动一个简单的.NET项目 我在网上找到了几个例子,似乎很容易掌握。FluentNHibernate和VARCHAR列,nhibernate,fluent-nhibernate,conventions,nvarchar,Nhibernate,Fluent Nhibernate,Conventions,Nvarchar,我正在用FluentNhibernate启动一个简单的.NET项目 我在网上找到了几个例子,似乎很容易掌握。 我意识到,如果让FluentNhibernate构建我的DB模式(Sql Server 2000),它会为我的字符串模型属性生成NVARCHAR字段 有人建议我可以添加一个约定来更改类型 这段代码非常有用: public class AnsiStringConvention : IPropertyConvention { private stat
我意识到,如果让FluentNhibernate构建我的DB模式(Sql Server 2000),它会为我的字符串模型属性生成NVARCHAR字段 有人建议我可以添加一个约定来更改类型 这段代码非常有用:
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");
}
}
}
现在我的DB字段是VARCHAR,正如我所预料的那样。我需要向我的类中添加一个组件,遵循我已经输入地址的DDD模式 在单独的类中,并将其添加到我的客户类中。
我已经为地址创建了一个单独的映射文件:
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);
}
}
公共类地址映射:组件映射
{
公共地址映射()
{
Map(x=>x.Number);
地图(x=>x.Street)
.长度(100);
地图(x=>x.City)
.长度(50);
地图(x=>x.邮政编码)
.长度(5);
}
}
现在,my Customer表的所有字段都是VARCHAR,但Address(Street、City和PostCode)组件的字段仍然创建为NVARCHAR。找到了一个解决方案,将
AnsiString
定义为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);
}
}
公共类地址映射:组件映射
{
公共地址映射()
{
//this.Map(x=>x.Number);
此.Map(x=>x.Street)
.CustomType(“AnsiString”)
.长度(100);
this.Map(x=>x.City)
.CustomType(“AnsiString”)
.长度(30);
this.Map(x=>x.State)
.CustomType(“AnsiString”)
.长度(20);
this.Map(x=>x.PostalCode)
.CustomType(“AnsiString”)
.长度(10);
this.Map(x=>x.Country)
.CustomType(“AnsiString”)
.长度(40);
}
}
很明显,Fluent Nhibernate中有一个bug:-非常感谢。我花了一整天的时间研究NHibernate和Fluent.NHibernate代码,试图找出为什么我的Fluent映射.CustomSqlType(“AnsiString”)仍然向sql server吐出一个NVarchar。这让我想知道CustomSqlType的意义是什么。