fluent-nhibernate - bool 值的 Fluent NHibernate 自定义类型约定

标签 fluent-nhibernate

尝试创建适用于所有 bool 属性的约定。

给定这个类:

public class Product
{
    public string Name { get; private set; }
    public bool IsActive { get; private set; }

    public Product(string name)
    {
        Name = name;
    }
}

我有一个像这样的映射:

public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Map(x => x.Name);
        Map(x => x.IsActive).CustomType<YesNoType>();
    }
}

我希望有一个约定可以做到这一点。到目前为止,我有:

public class YesNoTypeConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Apply(IPropertyInstance instance)
    {
        instance.CustomType<YesNoType>();
    }

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Property.DeclaringType == typeof(bool));
    }
}

我这样添加约定:

return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                            .ConnectionString(c => c.FromConnectionStringWithKey("dev"))
                            .AdoNetBatchSize(256)
                            .UseOuterJoin()
                            .QuerySubstitutions("true 1, false 0, yes 'Y', no 'N'"))
                .CurrentSessionContext<ThreadStaticSessionContext>()
                .Cache(cache => cache.ProviderClass<HashtableCacheProvider>())
                .ProxyFactoryFactory<ProxyFactoryFactory>()
                .Mappings(m => m.FluentMappings.AddFromAssembly(mappingAssembly)
                    .Conventions.Add(new ForeignKeyNameConvention(),
                                    new ForeignKeyContstraintNameConvention(),
                                    new TableNameConvention(),
                                    new YesNoTypeConvention(),
                                    new IdConvention()))
                .ExposeConfiguration(c => c.SetProperty("generate_statistics", "true"))
                .BuildSessionFactory();

但约定未被应用。 我认为问题出在 Apply 方法中的 Expect 标准。我尝试过不同的属性类型,例如 DeclaringType 和 PropertyType。

我应该查看 IPropertyInsepector 的哪个属性以确定它是否为 bool 值?

谢谢, 乔

最佳答案

criteria.Expect(x => x.Property.DeclaringType == typeof(bool));

// should be
criteria.Expect(x => x.Property.PropertyType == typeof(bool));

关于fluent-nhibernate - bool 值的 Fluent NHibernate 自定义类型约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8393439/

相关文章:

c# - 获取多对多关系的第一个元素

NHibernate.QueryException : could not resolve property

asp.net - Web API ASP.NET 中的 NHibernate : No session bound to the current context

c# - 使用 Fluent NHibernate 将 List<string> 映射到带分隔符的字符串

database - 遗留数据库、Fluent NHibernate 和测试我的映射

c# - 流利的 NHibernate : Cascade delete from one side only on Many-to-Many relationship

c# - 自定义类包含另一个自定义类时的 DataContractResolver/KnownType 问题

每个类加载 NHibernate 表 - 错误类型

使用空间查询时 Nhibernate 'Not all named parameters have been set'

c# - 验证 datetime fluent nhibernate 映射