“PropertyAccessException:无效转换”当尝试检索存储为 AnsiString 的枚举属性时

发布于 2024-12-07 01:39:20 字数 1527 浏览 1 评论 0原文

我需要将枚举存储在数据库中作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泡沫很甜 2024-12-14 01:39:20

本质上是这个问题的重复: 你怎么样在流畅的 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?

清风挽心 2024-12-14 01:39:20

使用这个就成功了(感谢@Firo):

Map(d => d.Type).CustomSqlType("varchar(50)");

查看 hbm(感谢@rbellamy 的建议)表明使用自定义 sql 类型不会覆盖映射中的枚举映射器,因此生成的 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="FluentNHibernate.Mapping.GenericEnumMapper`1[[Core.DocType, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], FluentNHibernate, Version=1.3.0.717, Culture=neutral, PublicKeyToken=8aa435e3cb308880">
      <column name="Type" sql-type="varchar(50)" />
    </property>
  </class>
</hibernate-mapping>

:一切都按其应有的方式进行。

Using this did the trick (thanks @Firo):

Map(d => d.Type).CustomSqlType("varchar(50)");

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:

<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="FluentNHibernate.Mapping.GenericEnumMapper`1[[Core.DocType, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], FluentNHibernate, Version=1.3.0.717, Culture=neutral, PublicKeyToken=8aa435e3cb308880">
      <column name="Type" sql-type="varchar(50)" />
    </property>
  </class>
</hibernate-mapping>

and all works as it should.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文