【问题标题】:How can I map a nullable enum in NHibernate?如何在 NHibernate 中映射一个可为空的枚举?
【发布时间】:2009-05-07 17:41:47
【问题描述】:

我无法使用 NHibernate 和 Fluent NHibernate 配置来持久化一个可为空的枚举。 NHibernate 尝试保存枚举的字符串表示,我得到错误

System.Data.SqlClient.SqlException: Conversion failed when converting the 
nvarchar value 'VGS' to data type tinyint.

属性定义为

public virtual CostContributor? ReplacementContributor { get; private set; }

映射是

Map(x => x.ReplacementContributor).CustomTypeIs(typeof(CostContributor?));

我已经尝试了 CustomTypeIs 和 CustomSqlTypeIs 的所有组合,包括替换 int?还是字节? CostContributor?,但没有任何效果。如果我将其设为不可为空的类型,它就可以正常工作。

是否可以在 NHibernate 中映射一个可为空的枚举?或者这是 NHibernate 中的错误或不支持的功能?

如果我无法完成这项工作,我将在我的枚举中添加一个未定义的值作为解决方法。

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate


    【解决方案1】:

    【讨论】:

    【解决方案2】:

    不确定如何正确执行此操作,但这是另一种解决方法:

    添加一个名为 CostContributorEntity 的类。该类将只有属性:ID,类型为 CostContributor。除非您愿意,否则您无需创建真正的表格。

    在消费类中将 ReplacementContributor 更改为 CostContributorEntity 类型,并将其映射为 References(x => x.ReplacementContributor);

    使用 session.Load(CostContributor.Blahblah1) 创建您可以分配给 ReplacementContributor 的 CostContributorEntity 实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多