8

我正在使用FluentNhibernate开始一个简单的 .NET 项目。
我遵循了我在 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 模式,我将地址放在一个单独的类中,并将其添加到我的客户类中。
我为地址创建了一个单独的映射文件:

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);
    }
}

现在,我的 Customer 表的所有字段都是 VARCHAR,但地址(街道、城市和邮政编码)组件的字段仍然创建为 NVARCHAR。

4

1 回答 1

12

找到了一个定义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);
    }
}
于 2010-08-26T10:49:56.570 回答