nhibernate - FluentNHibernate 和 VARCHAR 列

标签 nhibernate fluent-nhibernate conventions nvarchar

我正在开始一个简单的 .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/

相关文章:

nhibernate - 知道子集合计数而不用在 NHIbernate 中加载它们

nhibernate - Fluent NHibernate 对数千个对象执行单行选择以链接到父对象

c# - Fluent NHibernate 和好友关系

asp.net-mvc - 以下 Fluent NHibernate Mapping 有什么问题?

sql-server - 如何将 SQL Server 数据转储到 csv

objective-c - 使用中介来初始化对象

.net - .Net 4.0 中命名空间声明的错误或新约定

nhibernate - Fluent Nhibernate 1.0 - 在类和加入的子类之间指定外键约束名称

nhibernate - SchemaUpdate 不会删除表或删除列

asp.net - NHibernate 的 log4net 未将任何内容写入 ASP.NET 跟踪