代码之家  ›  专栏  ›  技术社区  ›  Garry Shutler

如何使用fluent NHibernate将枚举映射为int值?

  •  87
  • Garry Shutler  · 技术社区  · 17 年前

    问题说明了一切,默认情况下,它映射为 string int .

    我目前正在使用 PersistenceModel

    7 回复  |  直到 17 年前
        1
  •  84
  •   Julien    16 年前

    定义此约定的方式在以前有时会改变,现在是:

    public class EnumConvention : IUserTypeConvention
    {
        public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
        {
            criteria.Expect(x => x.Property.PropertyType.IsEnum);
        }
    
        public void Apply(IPropertyInstance target)
        {
            target.CustomType(target.Property.PropertyType);
        }
    }
    
        2
  •  45
  •   Garry Shutler    17 年前

    所以,正如前面提到的,把最新版本的Fluent-NHibernate从后备箱上取下来让我到达了我需要的地方。具有最新代码的枚举的映射示例如下:

    Map(quote => quote.Status).CustomTypeIs(typeof(QuoteStatus));
    

    自定义类型强制将其作为枚举实例处理,而不是使用 GenericEnumMapper<TEnum>

    实际上,我正在考虑提交一个补丁,以便能够在一个持久化字符串的枚举映射器和一个持久化int的枚举映射器之间进行更改,因为您应该能够将它设置为约定。


    这在我最近的活动中突然出现,在更新版本的Fluent NHibernate中,情况发生了变化,使这变得更容易。

    要将所有枚举映射为整数,现在可以创建如下约定:

    public class EnumConvention : IUserTypeConvention
    {
        public bool Accept(IProperty target)
        {
            return target.PropertyType.IsEnum;
        }
    
        public void Apply(IProperty target)
        {
            target.CustomTypeIs(target.PropertyType);
        }
    
        public bool Accept(Type type)
        {
            return type.IsEnum;
        }
    }
    

    那么您的映射只需是:

    Map(quote => quote.Status);
    

    将约定添加到Fluent NHibernate映射,如下所示;

    Fluently.Configure(nHibConfig)
        .Mappings(mappingConfiguration =>
        {
            mappingConfiguration.FluentMappings
                .ConventionDiscovery.AddFromAssemblyOf<EnumConvention>();
        })
        ./* other configuration */
    
        3
  •  40
  •   harriyott Erik Funkenbusch    13 年前

    ExampleEnum? ExampleProperty )! 它们需要单独检查。这就是新FNH样式配置的实现方式:

    public class EnumConvention : IUserTypeConvention
    {
        public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
        {
            criteria.Expect(x => x.Property.PropertyType.IsEnum ||
                (x.Property.PropertyType.IsGenericType && 
                 x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
                 x.Property.PropertyType.GetGenericArguments()[0].IsEnum)
                );
        }
    
        public void Apply(IPropertyInstance target)
        {
            target.CustomType(target.Property.PropertyType);
        }
    }
    
        4
  •  25
  •   Ufuk Hacıoğulları    13 年前

    这就是我如何将枚举属性映射为int值的方法:

    Map(x => x.Status).CustomType(typeof(Int32));
    

    为我工作!

        5
  •  1
  •   Community Mohan Dere    9 年前

    对于使用Fluent NHibernate和自动映射(以及可能的IoC容器)的用户:

    这个 IUserTypeConvention 朱利安 https://stackoverflow.com/a/1706462/878612

    public class EnumConvention : IUserTypeConvention
    {
        public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
        {
            criteria.Expect(x => x.Property.PropertyType.IsEnum);
        }
    
        public void Apply(IPropertyInstance target)
        {
            target.CustomType(target.Property.PropertyType);
        }
    }
    

    Fluent NHibernate自动映射配置可以如下配置:

        protected virtual ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(SetupDatabase)
                .Mappings(mappingConfiguration =>
                    {
                        mappingConfiguration.AutoMappings
                            .Add(CreateAutomappings);
                    }
                ).BuildSessionFactory();
        }
    
        protected virtual IPersistenceConfigurer SetupDatabase()
        {
            return MsSqlConfiguration.MsSql2008.UseOuterJoin()
            .ConnectionString(x => 
                 x.FromConnectionStringWithKey("AppDatabase")) // In Web.config
            .ShowSql();
        }
    
        protected static AutoPersistenceModel CreateAutomappings()
        {
            return AutoMap.AssemblyOf<ClassInAnAssemblyToBeMapped>(
                new EntityAutomapConfiguration())
                .Conventions.Setup(c =>
                    {
                        // Other IUserTypeConvention classes here
                        c.Add<EnumConvention>();
                    });
        }
    

    *然后 CreateSessionFactory 可以在诸如Castle Windsor之类的IoC中轻松使用(使用PersistenceFacility和安装程序)*

        Kernel.Register(
            Component.For<ISessionFactory>()
                .UsingFactoryMethod(() => CreateSessionFactory()),
                Component.For<ISession>()
                .UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
                .LifestylePerWebRequest() 
        );
    
        6
  •  0
  •   James Gregory    17 年前

    IUserType ,并使用 CustomTypeIs<T>() 在酒店地图上。

        7
  •  0
  •   Arkadas Kilic    12 年前

    您应该在DB表中将这些值保持为int/tinyint。要映射枚举,需要正确指定映射。请参见下面的映射和枚举示例,

    public class TransactionMap : ClassMap Transaction
    {
        public TransactionMap()
        {
            //Other mappings
            .....
            //Mapping for enum
            Map(x => x.Status, "Status").CustomType();
    
            Table("Transaction");
        }
    }
    

    枚举

    public enum TransactionStatus
    {
       Waiting = 1,
       Processed = 2,
       RolledBack = 3,
       Blocked = 4,
       Refunded = 5,
       AlreadyProcessed = 6,
    }