我正在开始一个简单的 .NET 项目 FluentNhibernate .
我遵循了在 Internet 上找到的几个示例,似乎很容易掌握。
我已经意识到,如果我让 FluentNhibernate 构建我的数据库架构(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");
}
}
}
现在我的数据库字段是 VARCHAR,正如我所料。
我需要按照 DDD 模式向我的类(class)添加一个组件
在一个单独的类中,并将其添加到我的 Customer 类中。
我为地址创建了一个单独的映射文件:
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);
}
}
现在,我的客户表的所有字段都是 VARCHAR,但地址(街道、城市和邮政编码)组件的字段仍创建为 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);
}
}
关于nhibernate - FluentNHibernate 和 VARCHAR 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3492868/