【问题标题】:set default value for enum in fluent nhibernate mapping在流畅的休眠映射中为枚举设置默认值
【发布时间】:2012-07-05 01:02:32
【问题描述】:

我有一个这样的映射。

public class MyObjectMap : ClassMap<MyObject> {
public MyObjectMap()
{
  Component(_ => _.MyItem, key =>
  {
    key.Map(x => x.MyItemValue).Column("COL");
    /** I want to set this value to a particular enum in this mapper **/
    key.Map(x => x.MyItemType).AssignSomeValue(MyEnum.MyValueType)
  });
}
}

如何将值设置为某些特定的项目类型。它是特定类型的组件。

【问题讨论】:

  • 您的意思是它应该始终具有相同的值而不需要数据库中的列吗?
  • 是的,x.MyItemType 的值在从数据库中读取时应该始终具有相同的值。该类在其他情况下使用,但在这种情况下,它是从数据库中读取的,我希望无论如何设置一个默认值。
  • 也许映射器有某种事件。 OnItemBeingRead 什么的。

标签: c# nhibernate mapping fluent


【解决方案1】:

IUserType 可以做到这一点

class ConstantValueUserType : IUserType
{
    NullSafeGet(IDataReader rd, string[] names, object owner)
    {
        return 5; // Constant Value
    }

    public object NullSafeSet(ICommand cmd, object value, int index)
    {
        // empty, we dont want to write
    }

    public SqlType[] SqlTypes { get { return new SqlType[0]; } }
}

【讨论】:

    【解决方案2】:
    key.Map(x => x.MyItemType).ReadOnly().Formula(((int)MyEnum.MyValueType).ToString()).CustomType<int>();
    

    【讨论】:

      猜你喜欢
      • 2011-06-11
      • 2011-11-15
      • 2010-12-08
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 2011-10-24
      • 2017-05-28
      相关资源
      最近更新 更多