复合 id 中的枚举 - 流畅的 NHibernate 1.2

发布于 2024-10-30 02:30:16 字数 726 浏览 4 评论 0原文

属于枚举类型且属于复合键一部分的属性的映射似乎已从 int 更改为 string,并且无法将其更改回来。

我有这个数据类:

public class Table5
{
    public virtual int Value { get; set; }

    public virtual Level Level { get; set; }

    public virtual string Name { get; set; }

    // Equality operators omitted
}

public enum Level
{
    Hi,
    Lo
}

通过这个映射:

public class Table5Map : ClassMap<Table5>
{
    public Table5Map()
    {
        Table("Table5");

        CompositeId()
            .KeyProperty(x => x.Value)
            .KeyProperty(x => x.Level);

        Map(x => x.Name);
    }
}

数据库中的“级别”列是一个整数。

这曾经有效,但在他的 Fluent 版本中,它尝试将字符串“Hi”和“Lo”写入“级别”列。

如何强制它映射到整数?

The mapping of a property that's of an enumerated type and is part of a composite key seems to have changed from int to string, and there's no way of changing it back.

I've got this data class:

public class Table5
{
    public virtual int Value { get; set; }

    public virtual Level Level { get; set; }

    public virtual string Name { get; set; }

    // Equality operators omitted
}

public enum Level
{
    Hi,
    Lo
}

with this mapping:

public class Table5Map : ClassMap<Table5>
{
    public Table5Map()
    {
        Table("Table5");

        CompositeId()
            .KeyProperty(x => x.Value)
            .KeyProperty(x => x.Level);

        Map(x => x.Name);
    }
}

The "Level" column in the database is an integer.

This used to work, but with his version of Fluent it attempts to write the strings "Hi" and "Lo" to the Level column.

How do I force it to map to an integer?

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

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

发布评论

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

评论(2

半山落雨半山空 2024-11-06 02:30:16

最简单的方法就是为键属性设置 Type:

CompositeId()
    .KeyProperty(x => x.Value)
    .KeyProperty(x => x.Level, m => m.Type(typeof(int)));

默认情况下,FNH 映射始终将枚举作为字符串。相反,在 FNH1.0 中,composite-id 内的 enum 默认映射为 int。可能是更改了,以修复更高版本中的这种不一致问题。

The simplest way is just set Type for the key property:

CompositeId()
    .KeyProperty(x => x.Value)
    .KeyProperty(x => x.Level, m => m.Type(typeof(int)));

FNH maps always enums as strings by default. On the contrary enum inside composite-id was mapped to int by default in FNH1.0. Probably it was changed in order to fix such inconsistency in higher versions.

擦肩而过的背影 2024-11-06 02:30:16

尝试对您的映射进行此更改:

/// snip ///
 CompositeId()
    .KeyProperty(x => x.Value)
    .KeyProperty(x => x.Level, c => c.ColumnName("Level").Type(typeof(Level)));
/// snip ///

这个“技巧”对我来说很有效,因为我必须处理具有许多组合键的遗留数据库。如果您需要非关键属性的等效“技巧”,看看这里接受的答案。祝你好运,愿你所有的数据库并不总是“遗留”!

Try this change to your mapping:

/// snip ///
 CompositeId()
    .KeyProperty(x => x.Value)
    .KeyProperty(x => x.Level, c => c.ColumnName("Level").Type(typeof(Level)));
/// snip ///

This "trick" worked for me in having to deal with a legacy database that had many composite keys. If you need the equivalent "trick" for non-key properties, look at the accepted answer here. Good luck and may all your databases not always be "legacy"!

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