我正在关注http://wiki.fluentnhibernate.org/Getting_started教程,用Fluent NHibernate创建我的第一个NHibernate项目
我有2张桌子
1)帐户与字段
Id
AccountHolderName
AccountTypeId
Run Code Online (Sandbox Code Playgroud)
2)带有字段的AccountType
Id
AccountTypeName
Run Code Online (Sandbox Code Playgroud)
现在,帐户类型可以是Savings或Current所以表AccountTypes存储2行1 - Savings 2 - Current
对于AccoutType表,我已经定义了枚举
public enum AccountType {
Savings=1,
Current=2
}
Run Code Online (Sandbox Code Playgroud)
对于Account表,我定义了实体类
public class Account {
public virtual int Id {get; private set;}
public virtual string AccountHolderName {get; set;}
public virtual string AccountType {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
流畅的nhibernate映射是:
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行解决方案,它提供了一个异常 - 的InnerException = {"(XmlDocument的)(2,4):XML验证错误:元素'类’在命名空间'瓮:NHibernate的映射-2.2’具有不完整的内容.期望的可能元素列表:'命名空间'中的元,子选择,缓存,同步,注释,tuplizer,id,composite-id'...
我想那是因为我还没有为AccountType指定任何映射.
问题是:
谢谢!
我用过这个如何使用流畅的NHibernate将枚举映射为int值?在过去映射,但我最近升级到NHibernate 3,这似乎不再起作用了.我在我的EnumConvention课程中放了断点,但它们没有被击中.命中数据库的查询将枚举作为字符串,这是默认配置.
这如何与NHibernate 3一起使用?
更新
以下是生成的映射文件的一部分:
<property name="ComponentType" type="FluentNHibernate.Mapping.GenericEnumMapper`1[[...ComponentType, ..., Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], FluentNHibernate, Version=1.1.0.0, Culture=neutral, PublicKeyToken=8aa435e3cb308880">
<column name="ComponentTypeId" />
</property>
Run Code Online (Sandbox Code Playgroud)
如果为枚举指定了GenericEnumMapper何时使用它似乎是正确的IUserTypeConvention.
这是我的惯例:
public class EnumConvention : IUserTypeConvention
{
public void Accept( IAcceptanceCriteria<IPropertyInspector> criteria )
{
criteria.Expect( e => e.Property.PropertyType.IsEnum );
}
public void Apply( IPropertyInstance instance )
{
instance.CustomType( instance.Property.PropertyType );
}
}
Run Code Online (Sandbox Code Playgroud) 我试图将我的模型中的枚举属性(System.DayOfWeek的实例)映射到整数数据库字段.模型中的其他枚举属性应该映射到字符串,所以我不希望定义约定.
我知道这应该可以使用流畅的映射,如:
Map(x => x.DayOfWeek).CustomType<int>();
Run Code Online (Sandbox Code Playgroud)
事实上,乍一看这似乎有效.
但是,我注意到每次刷新会话时都会更新具有以这种方式映射的属性的实体实例,即使没有对它们进行任何修改.
为了找出导致此刷新的原因,我设置了一个IPreUpdateEventListener,并检查了该实体的OldState和State.请参见附图.在OldState中,相关对象是int,而在State中它是DayOfWeek.
如果我使用未指定类型属性的HBM XML映射,则不会出现此问题.
所以...
这是GenericEnumMapper中的错误还是缺点?有没有办法告诉FNH映射不要在生成的HBM上指定任何类型属性?如果没有,我可以指定NH用于枚举的默认类型(以及那是什么)?

我有一个域实体,它有一个标记的枚举作为属性.标记的枚举是这些实体的目标受众.然后,用户具有他们应该看到的实体的标记枚举值.我试图找出正确的表达式来选择满足用户目标受众的实体.
public class File
{
public virtual TargetAudience TargetAudience { get; set; }
}
[Flags]
public enum TargetAudience
{
Audience1 = 1,
Audience2 = 2,
Audience3 = 4,
Audience4 = 8
}
Run Code Online (Sandbox Code Playgroud)
表达式:(这在a上执行时有效IList<File>,但对数据库的查询不起作用.)
public Expression<Func<File, bool>> Expression
{
get { return ((x.TargetAudience & UserTargetedAudience) > 0); }
}
Run Code Online (Sandbox Code Playgroud)
任何的意见都将会有帮助.
我有一个类具有枚举类型,指示消息类型是电子邮件还是短信.枚举类型定义为:
public enum ReminderType
{
Email = 1,
Sms = 2
}
Run Code Online (Sandbox Code Playgroud)
使用此类型的类看起来像:
public class Reminder : EntityBase
{
public virtual string Origin { get; set; }
public virtual string Recipient { get; set; }
public virtual ReminderType Type { get; set; }
public virtual Business Business { get; set; }
public virtual DateTime Created { get; set; }
public Reminder()
{
Created = DateTime.UtcNow;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将类型为Reminder的实体持久保存到数据库时,我收到以下错误:
System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting the nvarchar value 'Email' to data …Run Code Online (Sandbox Code Playgroud) 我enum在流畅的NHibernate中映射时遇到了一些问题.我知道这个问题已被多次询问,但我找不到任何对我有用的解决方案.我是NHibernate的新手,看起来我可能错过了一些简单而愚蠢的东西.这是我的代码.
public class DBPublication
{
public virtual int pub_id { get; set; }
public virtual PublicationStatuses status { get; set; }
...
}
public enum PublicationStatuses
{
on_moderation,
active,
...
}
public class DBPublicationMap : ClassMap<DBPublication>
{
public DBPublicationMap()
{
Table("content.publications");
Id(x => x.pub_id).GeneratedBy.Sequence("content.pub_sq");
Map(x => x.status);
...
}
}
Run Code Online (Sandbox Code Playgroud)
postgres枚举类型
CREATE TYPE content.enum_publication_status AS ENUM('on_moderation', 'active', ...);
Run Code Online (Sandbox Code Playgroud)
但是当我试图保存时,postgres会抛出这个
column "status" is of type content.enum_publication_status but expression is of type text
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
我需要使用NHibernate将具有Enums列表的类映射到db表
这是对象
public class Driver : IIdentity
{
private IList<Licence> licences;
/// <summary>
/// The drivers licences
/// </summary>
public virtual IList<Licence> Licences
{
get
{
return this.licences;
}
set
{
this.licences = value;
}
}
..... rest of the class ....
}
//the enum
public enum Licence
{
FivePersonCar = 5,
SixPersonCar = 6
}
Run Code Online (Sandbox Code Playgroud)
----------------这里是DB表
TABLE [dbo].[DriverLicence](
[DriverId] [int] NOT NULL,
[Level] [int] NOT NULL)
TABLE [dbo].[Driver](
[DriverId] [int] NOT NULL,
[Name] [varchar](150) NULL)
-------------这是我的驱动程序的Fluent地图
public class DriverMap …Run Code Online (Sandbox Code Playgroud) 我有一堆我创建的POCO,我想创建一个持久层.事实上,我真的不在乎数据是如何存储在SQL Server中的,我只是希望它存储起来.换句话说,我想告诉ORM工具,"这里有一些POCO类,保存它们." 并且不必做任何事情.是否有任何可以做到这一点的C#ORM工具?我一直在努力让Fluent NHibernate工作,而且Subsonic不支持人际关系,这使得像"为一个帖子获取所有评论"这样的事情非常困难.它需要能够自动生成数据库模式,而不必设置一堆属性和诸如此类的东西.
如何进行HQL或Criteria搜索(后者是首选)涉及用作标志的枚举.换句话说,我有一个持久的枚举属性,存储某种标志.我想查询所有设置了这些标志之一的记录.使用Eq当然不会起作用,因为只有那是唯一的标志才会成立.
使用Criteria API解决这个问题是最好的,但是如果只使用HQL这样做也很好.