让 NHibernate 生成 HiLo 字符串 ID
我们有一个大型系统,通过 Unity 与其数据源 (Navision) 松散绑定 - 我们有机会将其换出并拥有自己的数据库。
因此,我们环顾四周,非常喜欢 Fluent NHibernate 的外观 - 我们正在尝试进行概念验证并更换一些服务。
我们想要使用 NHibernates HiLo 算法 - 不幸的是,我们从 Navision 继承了字符串 ID,它为其 ID 加上前缀(例如 COL00001),因此为了匹配接口,我们需要使用字符串 ID。
有谁知道我如何得到类似...
Id(x => x.ID).GeneratedBy.HiLo("100");
在 ID 是字符串的情况下工作?我们目前正在获取 Identity 必须是 int、long 等
谢谢,
Andy
------ 更新 ------
我尝试了建议的文章中的示例,但此功能已从 Fluent NHibernate 的更高版本中删除 - 那里然而是一个 .Custom - 但我似乎无法让它工作!
public class ManufacturerMap : ClassMap<Manufacturer>
{
public ManufacturerMap()
{
Id(x => x.ID).GeneratedBy.Custom(typeof(StringTableHiLoGenerator));
Map(x => x.Name);
}
}
public class StringTableHiLoGenerator : TableHiLoGenerator
{
public override object Generate(ISessionImplementor session, object obj)
{
return base.Generate(session, obj).ToString();
}
}
We've got a large system that's loosely bound to its data source (Navision) via Unity - we're getting the opportunity to swap it out and have our own database.
So we've had a look around and really like the look of Fluent NHibernate - we're trying to get a proof of concept going and swap out a couple of the services.
We want to use NHibernates HiLo algorithm - Unfortunately we've inherited string ID's from Navision which prefixs its ID's (example COL00001) so to match the Interface we need to use string Id's.
Does anyone know how I'd get something like ...
Id(x => x.ID).GeneratedBy.HiLo("100");
working where ID is a string? We're currently getting Identity must be int, long etc
Thanks,
Andy
------ Update ------
I tried the example in the article suggested but this functionality has been removed from later versions of Fluent NHibernate - there is however a .Custom - but I can't seem to get it working!
public class ManufacturerMap : ClassMap<Manufacturer>
{
public ManufacturerMap()
{
Id(x => x.ID).GeneratedBy.Custom(typeof(StringTableHiLoGenerator));
Map(x => x.Name);
}
}
public class StringTableHiLoGenerator : TableHiLoGenerator
{
public override object Generate(ISessionImplementor session, object obj)
{
return base.Generate(session, obj).ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您无法让标准的 HiLo 生成器与字符串一起使用。看一下创建一个自定义 id 生成器(可以是带有字符串的 hilo):
http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.aspx
有关您更新的更新
我在流利的维基中找不到任何与此相关的正确文档。不过,您可以尝试这个通用方法,而不是您正在使用的方法:
这有效吗?如果没有,我认为如果您在 Fluent-nhibernate 邮件列表上发帖,您可能会得到最快的回复:
http: //groups.google.com/group/ Fluent-nhibernate
I don't think you will manage to get the standard HiLo generator working with a string. Take a look at creating a custom id generator (which could be a hilo with a string):
http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.aspx
UPDATE regarding you update
I can't find any proper documentation regarding this in the fluent wiki. You could try this generic method though, rather than the method you are using:
Does that work? If not I think your quickest response might come if you post on the fluent-nhibernate mailing list:
http://groups.google.com/group/fluent-nhibernate
发布的答案也适用于我的情况,我有一个字符串作为带有自定义 ID 生成器的主键,并且 Nhiberate 抛出错误:
“类型不是 ValueTypeType
参数名称:类型”。
The posted answer also worked in my case where i had a string as Primary Key with Custom Id Generator and Nhiberate was throwing the error:
"type is not a ValueTypeType
Parameter name: type".
终于破解了...感谢您的帮助 - 如果有人感兴趣,这里是解决方案...
注意:在
Configure
方法中IType
必须作为int
传递给基类。Finally cracked it ... thanks for your assistance - here's the solution in case anyone's interested...
Note: that in the
Configure
method theIType
has to be passed to the base as anint
.