为什么Fluent的AutoMapping';s NHibernate忽略枚举类型

本文关键字:NHibernate 枚举 类型 Fluent AutoMapping 为什么 | 更新日期: 2023-09-27 18:29:29

我有以下枚举类型:

public enum EnumType
{
    E1,
    E2
}

用于以下类别:

public class X
{
    public virtual int? Id { get; set; }
    public virtual EnumType EnumProperty { get; set; }
    public virtual string S { get; set; }
}

我想使用NHibernate将这种类型的实例持久化到数据库中。为了避免编写样板代码,我尝试使用如下自动映射功能:

private ISessionFactory CreateSessionFactory()
{
    var mappings = AutoMap
        .AssemblyOf<Domain.X>(new MyAutoMappingConfiguration());
    this.NHibernateConfiguration = Fluently
        .Configure()
        .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2012.ConnectionString(
                b => b.FromConnectionStringWithKey("x")))
        .Mappings(m => m.AutoMappings.Add(mappings))
        .BuildConfiguration();
    return this.NHibernateConfiguration.BuildSessionFactory();
}

其中MyAutoMappingConfiguration如下所示:

public class MyAutoMappingConfiguration: FluentNHibernate.Automapping.DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        return type.Namespace == "Domain";
    }
    public override bool IsComponent(Type type)
    {
        return type.Name == "EnumType";
    }
}

当我使用此配置生成的模式来创建数据库时:

new SchemaExport(this.sessionProvider.NHibernateConfiguration)
    .Execute(true, true, false);

正在生成并执行以下脚本:

if exists (select * from dbo.sysobjects where id = object_id(N'[X]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [X]
create table [X] (
    Id INT not null,
   S NVARCHAR(255) null,
   primary key (Id)
)

为什么忽略属性EnumProperty

当我为X添加一个显式映射,或者(似乎等效的)为自动映射添加一个覆盖时,如下所示:

var mappings = AutoMap
    .AssemblyOf<Domain.X>(new MyAutoMappingConfiguration())
    .Override<Domain.X>(m =>
    {
        m.Table("X");
        m.Id(x => x.Id);
        m.Map(x => x.EnumProperty);     // this works
        m.Map(x => x.S);
    });

脚本生成正确:

if exists (select * from dbo.sysobjects where id = object_id(N'[X]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [X]
create table [X] (
    Id INT not null,
   EnumProperty NVARCHAR(255) null,
   S NVARCHAR(255) null,
   primary key (Id)
)

这表明NHibernate正确映射所呈现枚举的能力似乎没有什么问题。为什么自动映射不能解决这个问题?


当我将以下方法添加到MyAutoMappingConfiguration时:

public override bool ShouldMap(Member member)
{
    var result = base.ShouldMap(member);
    return result;
}

并且放置断点,对于EnumProperty成员,resulttrue,稍后会以某种方式被忽略。

为什么Fluent的AutoMapping';s NHibernate忽略枚举类型

根据我的经验,枚举是开箱即用映射的,根本不需要做任何额外的事情(没有自定义类型或组件)。

因此,有两个问题:

  • IsComponent不应该指示有问题的枚举是一个组件:

    public override bool IsComponent(Type type)
    {
        return base.IsComponent(type);
    }
    

    (或者干脆取消实施)

  • ShouldMap不应该指示枚举需要显式映射。它无论如何都会被映射,因为它是一个属性。例如:

    public override bool ShouldMap(Member member)
    {
        return base.ShouldMap(member) && member.CanWrite &&
               !member.MemberInfo.IsDefined(typeof(NotMappedAttribute), false);
    }
    

    在您的情况下,如果枚举在同一个命名空间中,您应该执行以下操作:

    public override bool ShouldMap(Type type)
    {
        return type.Namespace == "Domain"
            && !type.IsEnum;
    }
    

    (这有点违背直觉)