开发者

"PropertyAccessException: Invalid Cast" when trying to retrieve an enum property stored as an AnsiString

开发者 https://www.devze.com 2023-04-09 02:46 出处:网络
I need to store my enums in the database as varchar instead of nvarchar, so I am using the \"AnsiString\" mapping as follows:

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" />
    <开发者_如何学C;/property>
    <property name="Type" type="AnsiString">
      <column name="Type" />
    </property>
  </class>
</hibernate-mapping>


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号