如何在NHibernate中映射可以为空的枚举?

时间:2021-10-14 11:23:21

I have been unable to persist a nullable enum using NHibernate with Fluent NHibernate configuration. NHibernate attempts to save a string representation of the enum and I get the error

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

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

The property is defined as

该属性定义为

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

and the mapping is

并且映射是

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

I've tried every combination of CustomTypeIs and CustomSqlTypeIs, including substituting int? or byte? for CostContributor?, but nothing has worked. It works fine if I make it a non-nullable type.

我已经尝试过CustomTypeIs和CustomSqlTypeIs的每个组合,包括替换int?还是字节?对于CostContributor ?,但没有任何效果。如果我把它变成一个不可为空的类型,它工作正常。

Is it possible to map a nullable enum in NHibernate? Or is this a bug or unsupported feature in NHibernate?

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

If I can't make this work I'm going to add an Undefined value to my enum as a workaround.

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

2 个解决方案

#1


This was a bug and it's been fixed.

这是一个错误,它已被修复。

#2


Not sure how to do it properly but here's another workaround:

不知道如何正确地做到这一点,但这是另一种解决方法:

Add a class called CostContributorEntity. The class will have only property: ID, of type CostContributor. You don't need to create a real table unless you want to.

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

In the consuming classes change ReplacementContributor to be of type CostContributorEntity, and map it as References(x => x.ReplacementContributor);

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

Use session.Load(CostContributor.Blahblah1) to create instance of CostContributorEntity you can assign to ReplacementContributor.

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

#1


This was a bug and it's been fixed.

这是一个错误,它已被修复。

#2


Not sure how to do it properly but here's another workaround:

不知道如何正确地做到这一点,但这是另一种解决方法:

Add a class called CostContributorEntity. The class will have only property: ID, of type CostContributor. You don't need to create a real table unless you want to.

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

In the consuming classes change ReplacementContributor to be of type CostContributorEntity, and map it as References(x => x.ReplacementContributor);

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

Use session.Load(CostContributor.Blahblah1) to create instance of CostContributorEntity you can assign to ReplacementContributor.

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