尝试创建适用于所有 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/