如何在 MySql 数据库中使用 guid.comb 策略
是否可以使用 guid.comb 策略通过 Nhibernate 与 Mysql Db 进行身份生成?
当我使用它时,
mapping.Id(x => x.Id)
.Column("row_guid")
.CustomType(typeof(string))
.GeneratedBy.GuidComb()
.Length(36);
我最终得到的是
----> 系统.InvalidOperationException: 身份类型必须是 Guid
在MySql场景下有没有办法克服这个障碍?
编辑:
我在 guid 和 int 之间没有选择。这是来自 MSSql 的旧数据库的端口
Is it possible to use the guid.comb strategy for identity generation with Mysql Db using Nhibernate?
When I use it as
mapping.Id(x => x.Id)
.Column("row_guid")
.CustomType(typeof(string))
.GeneratedBy.GuidComb()
.Length(36);
I end up with a
---->
System.InvalidOperationException :
Identity type must be Guid
Is there a way to overcome this obstacle in the MySql scenario?
Edit:
I don’t have a choice between guid and int. This is a port of a legacy db from MSSql
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是常见问题,尤其是在将 MSSql 应用程序移植到 MySql 时。
正如 David 所说,实现一个简单的 CustomIdGenerator,它是 GuidCombGenerator 为您提供字符串形式的 Guid。
并在映射中将其指定为
This is common problem especially when porting MSSql applications to MySql.
As David said,implement a simple CustomIdGenerator which is a wrapper over GuidCombGenerator that gives you the Guid as a string.
And in the mapping specify it as
我不相信为主键使用不受支持的数据类型是否明智,但如果您确实需要这样做,那么您可以尝试编写一个 NHibernate 用户类型,将其属性公开为 Guid,但将其保留到数据库中一个字符串。本例中的问题似乎是属性本身被定义为 guid.comb 策略所期望的 System.Guid 以外的数据类型。
我不能保证它仍然不会出错,但如果可能的话,这是让它工作的唯一方法。如果您是 NHibernate 用户类型的新手,有一个抽象基类可以为您处理一些苦差事 此处,带有示例实现类。您应该能够遵循这个示例。
I'm not convinced of the wisdom of using an unsupported data type for your primary key, but if you really do need to do this then you could try writing an NHibernate user type that exposes its property as a Guid but persists to the database as a string. The problem in this case seems to be that the property itself is defined as a data type other than
System.Guid
, which the guid.comb strategy expects.I can't guarantee that it still won't error, but it's the only way you'll get it to work if it is possible. If you're new to NHibernate user types, there is an abstract base class that takes care of some of the drudge work for you here, with an example implementation class. You should be able to follow this example.
只需使用
System.Guid
作为属性的类型即可。O/RM 与映射有关,因此即使您的数据库本身不支持给定类型,您仍然可以在域模型中使用它。为了与 MySQL 兼容,列的基础类型应为
BINARY(16)
。Just can use
System.Guid
as the type of the property.O/RM is about mapping, so even though your database doesn't support a given type natively, you can still use this in your domain model. The underlying type of your column should be
BINARY(16)
, for MySQL compatibility.