【问题标题】:Fluent NHibernate - override type for one specific property on one specific class?Fluent NHibernate - 一个特定类的一个特定属性的覆盖类型?
【发布时间】:2011-03-31 21:23:25
【问题描述】:

我有一个类,它有一个密码属性,我想将它加密存储在数据库中。该属性是一个字符串类型,我有一个自定义类型 EncryptedStringType,我希望 NHibernate 使用它来将其映射到数据库。这是我的相关自动映射代码:

var mappings = AutoMap.AssemblyOf<Business>()
    .Where(x=>x.IsSubclassOf(typeof(EntityBase)))
    .IgnoreBase(typeof(EntityBase))
    .Conventions.Add
        (
            ConventionBuilder.Id.Always(x =>
                x.GeneratedBy.HiLo(HILO_TABLE, HILO_COLUMN, HILO_MAX_LO)),
            ConventionBuilder.HasMany.Always(x => x.Cascade.AllDeleteOrphan()),
            Table.Is(o => Inflector.Pluralize(o.EntityType.Name)),
            PrimaryKey.Name.Is(o => "Id"),
            ForeignKey.EndsWith("Id"),
            DefaultLazy.Always(),
            DefaultCascade.All()
        );

不过,我无法弄清楚重写 Business 类的 UserPassword 属性类型的语法。我认为我应该能够使用以下覆盖来做一些事情:

mappings.Override<Business>(map=> /* Not sure what to do here */);

感谢任何帮助。

【问题讨论】:

    标签: fluent-nhibernate


    【解决方案1】:

    自己找到了答案。

    mappings.Override<Business>(map =>
    {
        map.Map(x => x.UserPassword).CustomType<EncryptedStringType>();
    });
    

    【讨论】:

      【解决方案2】:

      您总是可以创建一个映射覆盖类。仍然可以应用任何约定,但您基本上可以指定类似于覆盖默认约定的 ClassMap 的映射。

      使用对 mappings.Override() 的调用,它看起来像:

      mappings.Override<Business>(map=>map.Map(x=>x.UserPassword).CustomType(typeof(EncryptedStringType)));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-19
        • 2011-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多