“PropertyAccessException:无效转换”当尝试检索存储为 AnsiString 的枚举属性时
我需要将枚举存储在数据库中作为 varchar 而不是 nvarchar,因此我使用“AnsiString”映射,如下所示:
public class Document
{
public virtual int Id { get; set; }
public virtual string Content { get; set; }
public virtual DocType Type { get; set; }
}
public enum DocType
{
Word,
Excel
}
public class DocumentMap : ClassMap<Document>
{
public DocumentMap()
{
Id(d => d.Id);
Map(d => d.Content);
Map(d => d.Type).CustomType("AnsiString");
}
}
保存到数据库工作正常,但在检索时收到错误: NHibernate.PropertyAccessException:无效转换(检查映射是否属性类型不匹配); Core.Document 的设置器
当我从映射中删除 CustomType("AnsiString") 时,它工作正常。
有什么建议吗?
这是 hbm:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="Core.Document, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Document`">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<property name="Content" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Content" />
</property>
<property name="Type" type="AnsiString">
<column name="Type" />
</property>
</class>
</hibernate-mapping>
I need to store my enums in the database as varchar instead of nvarchar, so I am using the "AnsiString" mapping as follows:
public class Document
{
public virtual int Id { get; set; }
public virtual string Content { get; set; }
public virtual DocType Type { get; set; }
}
public enum DocType
{
Word,
Excel
}
public class DocumentMap : ClassMap<Document>
{
public DocumentMap()
{
Id(d => d.Id);
Map(d => d.Content);
Map(d => d.Type).CustomType("AnsiString");
}
}
Saving to the database works fine, but when it comes to retrieval I receive an error:
NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of Core.Document
It works fine when I remove the CustomType("AnsiString") from the mapping.
Any suggestions?
Here's the hbm:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="Core.Document, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Document`">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<property name="Content" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Content" />
</property>
<property name="Type" type="AnsiString">
<column name="Type" />
</property>
</class>
</hibernate-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本质上是这个问题的重复: 你怎么样在流畅的 nhibernate 中将枚举映射为字符串?
区别在于您尝试使用非 Unicode 字符串(MSSQL 中的 NVARCHAR)作为基础数据类型。
那么,FNH 生成的底层映射是什么样的呢?
Essentially a duplicate of this question: How do you map an enum as string in fluent nhibernate?
The difference is that you're trying to use a Non-Unicode string (NVARCHAR in MSSQL) as the underlying data type.
So, what does the underlying mapping generated by FNH look like?
使用这个就成功了(感谢@Firo):
查看 hbm(感谢@rbellamy 的建议)表明使用自定义 sql 类型不会覆盖映射中的枚举映射器,因此生成的 hbm 如下所示
:一切都按其应有的方式进行。
Using this did the trick (thanks @Firo):
Looking at the hbm (thanks @rbellamy for the suggestion) shows that using the custom sql type doesn't override the enum mapper from the mappings, so the resulting hbm looks like this:
and all works as it should.