Fluent NHibernate 插入 Null FK

发布于 2024-12-11 11:16:51 字数 410 浏览 0 评论 0原文

我有一个带有可为空 FK 约束的表映射。在我的流畅映射中,我正在做类似这样的事情:

public enum PlayerPosition
{
        None = 0,
        Forward = 1
//etc
}

Entity

public virtual PlayerPosition? Position { get; set; }

Map(x => x.Position).Column("PlayerPositionId").CustomType< PlayerPosition>();

我希望发生的是当 PlayerPosition 设置为“None”时,Nhibernate 将插入 null。我不知道如何实现这一点。

I have a table mapping with nullable FK constraint. In my fluent mapping I am doing something like so:

public enum PlayerPosition
{
        None = 0,
        Forward = 1
//etc
}

Entity

public virtual PlayerPosition? Position { get; set; }

Map(x => x.Position).Column("PlayerPositionId").CustomType< PlayerPosition>();

What I would like to happen is when PlayerPosition is set to "None" Nhibernate will insert null. I am not sure how to make that happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2024-12-18 11:16:51

我会选择 IUserType:

public virtual PlayerPosition Position { get; set; }

Map(x => x.Position).Column("PlayerPositionId").CustomType<PlayerPositionUserType>();

class PlayerPositionUserType : IUserType
{

    public object NullSafeGet(IDBReader reader, string[] names, object owner)
    {
        int? positionvalue = NHibernateUtil.Int32.NullSafeGet(reader, names[0]);
        return (positionvalue.HasValue) ? (PlayerPosition)positionvalue : PlayerPosition.None;
    }

    public void NullSafeSet(IDBCommand cmd, object value, int index)
    {
        var position = (PlayerPosition)value;
        if (position == PlayerPosition.None)
            NHibernateUtil.Int32.NullSafeSet(cmd, null, index);
        else
            NHibernateUtil.Int32.NullSafeSet(cmd, (int)position, index);
    }

    public Type ReturnType
    {
        get { return typeof(PlayerPosition); }
    }

    public SqlType[] SqlTypes
    {
        get { return new [] { SqlTypeFactory.Int32 } }
    }
}

i would go for IUserType:

public virtual PlayerPosition Position { get; set; }

Map(x => x.Position).Column("PlayerPositionId").CustomType<PlayerPositionUserType>();

class PlayerPositionUserType : IUserType
{

    public object NullSafeGet(IDBReader reader, string[] names, object owner)
    {
        int? positionvalue = NHibernateUtil.Int32.NullSafeGet(reader, names[0]);
        return (positionvalue.HasValue) ? (PlayerPosition)positionvalue : PlayerPosition.None;
    }

    public void NullSafeSet(IDBCommand cmd, object value, int index)
    {
        var position = (PlayerPosition)value;
        if (position == PlayerPosition.None)
            NHibernateUtil.Int32.NullSafeSet(cmd, null, index);
        else
            NHibernateUtil.Int32.NullSafeSet(cmd, (int)position, index);
    }

    public Type ReturnType
    {
        get { return typeof(PlayerPosition); }
    }

    public SqlType[] SqlTypes
    {
        get { return new [] { SqlTypeFactory.Int32 } }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文