问题确实如此,默认情况下它会映射为string
,但我需要将其映射为int
。如何将一个枚举映射为一个int值和流畅的NHibernate?
我目前使用PersistenceModel
来设置我的约定,如果这有什么区别。提前致谢。
更新 发现从中继获取最新版本的代码解决了我的困境。
问题确实如此,默认情况下它会映射为string
,但我需要将其映射为int
。如何将一个枚举映射为一个int值和流畅的NHibernate?
我目前使用PersistenceModel
来设置我的约定,如果这有什么区别。提前致谢。
更新 发现从中继获取最新版本的代码解决了我的困境。
的方法来定义这个约定有时前改变了,它现在:
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum);
}
public void Apply(IPropertyInstance target)
{
target.CustomType(target.Property.PropertyType);
}
}
您可以创建NHibernate IUserType
,并在属性映射上使用CustomTypeIs<T>()
指定它。
所以,如前所述,将最新版本的流利NHibernate从主干中取出,让我到了需要的地方。与最新的代码中的枚举的示例映射是:
Map(quote => quote.Status).CustomTypeIs(typeof(QuoteStatus));
定制类型迫使它作为枚举的实例来处理,而不是使用GenericEnumMapper<TEnum>
。
我实际上正在考虑提交一个修补程序,以便能够在保持字符串的枚举映射器和保持int的枚举映射器之间进行切换,因为这看起来应该是您应该能够设置为约定的东西。
这出现在我最近的活动上,并且在新版本的Fluent NHibernate中,事情已经发生了变化,以使这更容易。
要使所有枚举被映射为整数,你现在就可以创造像这样一个约定:
public class EnumConvention : IUserTypeConvention
{
public bool Accept(IProperty target)
{
return target.PropertyType.IsEnum;
}
public void Apply(IProperty target)
{
target.CustomTypeIs(target.PropertyType);
}
public bool Accept(Type type)
{
return type.IsEnum;
}
}
然后你的映射只有是:
Map(quote => quote.Status);
您的约定添加到您的流利像这样的NHibernate映射;
Fluently.Configure(nHibConfig)
.Mappings(mappingConfiguration =>
{
mappingConfiguration.FluentMappings
.ConventionDiscovery.AddFromAssemblyOf<EnumConvention>();
})
./* other configuration */
,默认为“int mode”。谁将枚举持久化为字符串? – 2009-02-17 10:40:18
不要忘记为空的枚举(如ExampleEnum? ExampleProperty
)!他们需要分开检查。这是它是如何与新的FNH风格的配置来完成:
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum ||
(x.Property.PropertyType.IsGenericType &&
x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
x.Property.PropertyType.GetGenericArguments()[0].IsEnum)
);
}
public void Apply(IPropertyInstance target)
{
target.CustomType(target.Property.PropertyType);
}
}
这是怎么了,我映射一个枚举属性有一个int值:
Map(x => x.Status).CustomType(typeof(Int32));
对我的作品!
对于使用功能NHibernate与自动映射(以及潜在IoC容器)的那些:
的IUserTypeConvention
是@于连的回答以上:https://stackoverflow.com/a/1706462/878612
public class EnumConvention : IUserTypeConvention
{
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum);
}
public void Apply(IPropertyInstance target)
{
target.CustomType(target.Property.PropertyType);
}
}
的功能NHibernate自动映射配置可以像这样配置:
protected virtual ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(SetupDatabase)
.Mappings(mappingConfiguration =>
{
mappingConfiguration.AutoMappings
.Add(CreateAutomappings);
}
).BuildSessionFactory();
}
protected virtual IPersistenceConfigurer SetupDatabase()
{
return MsSqlConfiguration.MsSql2008.UseOuterJoin()
.ConnectionString(x =>
x.FromConnectionStringWithKey("AppDatabase")) // In Web.config
.ShowSql();
}
protected static AutoPersistenceModel CreateAutomappings()
{
return AutoMap.AssemblyOf<ClassInAnAssemblyToBeMapped>(
new EntityAutomapConfiguration())
.Conventions.Setup(c =>
{
// Other IUserTypeConvention classes here
c.Add<EnumConvention>();
});
}
*那么CreateSessionFactory
可以很容易地用于Castle Windsor等IoC(使用PersistenceFacility和installer)。 *
Kernel.Register(
Component.For<ISessionFactory>()
.UsingFactoryMethod(() => CreateSessionFactory()),
Component.For<ISession>()
.UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
.LifestylePerWebRequest()
);
您应该将值保存为int/tinyint在您的数据库表中。为了映射你的枚举,你需要正确指定映射。请参阅下面的映射和枚举样品,
映射类
public class TransactionMap : ClassMap Transaction { public TransactionMap() { //Other mappings ..... //Mapping for enum Map(x => x.Status, "Status").CustomType(); Table("Transaction"); } }
枚举
public enum TransactionStatus { Waiting = 1, Processed = 2, RolledBack = 3, Blocked = 4, Refunded = 5, AlreadyProcessed = 6, }
如果你自己解决了这个问题,你应该回答这个问题,然后将其标记为正确的回答以便将来的搜索者可以找到它。 – 2009-01-13 16:17:02
你可以请发布答案吗? – mxmissile 2009-01-13 21:51:28
完成家伙。抱歉耽搁了。我真的不知道我应该怎么处理这个问题,因为我只需要最新版本的库。 – 2009-01-14 08:46:36