复合 id 中的枚举 - 流畅的 NHibernate 1.2
属于枚举类型且属于复合键一部分的属性的映射似乎已从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法就是为键属性设置 Type:
默认情况下,FNH 映射始终将枚举作为字符串。相反,在 FNH1.0 中,composite-id 内的 enum 默认映射为 int。可能是更改了,以修复更高版本中的这种不一致问题。
The simplest way is just set Type for the key property:
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.
尝试对您的映射进行此更改:
这个“技巧”对我来说很有效,因为我必须处理具有许多组合键的遗留数据库。如果您需要非关键属性的等效“技巧”,看看这里接受的答案。祝你好运,愿你所有的数据库并不总是“遗留”!
Try this change to your mapping:
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"!