如何在 MySql 数据库中使用 guid.comb 策略

发布于 2024-08-16 07:05:22 字数 477 浏览 2 评论 0原文

是否可以使用 guid.comb 策略通过 Nhibernate 与 Mysql Db 进行身份生成?

当我使用它时,

   mapping.Id(x => x.Id)
        .Column("row_guid")
        .CustomType(typeof(string))
        .GeneratedBy.GuidComb()
        .Length(36);

我最终得到的是

----> 系统.InvalidOperationException: 身份类型必须是 Guid

在MySql场景下有没有办法克服这个障碍?

编辑:
我在 guidint 之间没有选择。这是来自 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 技术交流群。

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

发布评论

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

评论(3

无声无音无过去 2024-08-23 07:05:22

这是常见问题,尤其是在将 MSSql 应用程序移植到 MySql 时。
正如 David 所说,实现一个简单的 CustomIdGenerator,它是 GuidCombGenerator 为您提供字符串形式的 Guid。

using NHibernate.Engine;
using NHibernate.Id;

namespace NHibernateMaps
{
  public class GuidStringGenerator : IIdentifierGenerator
  {
    public object Generate(ISessionImplementor session, object obj)
    {
      return new GuidCombGenerator().Generate(session, obj).ToString();
    }

  }
}

并在映射中将其指定为

 mapping.Id(x => x.Id)
    .Column("row_id")
    .CustomType(typeof(string))
    .GeneratedBy.Custom(typeof(GuidStringGenerator))
    .Length(36);

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.

using NHibernate.Engine;
using NHibernate.Id;

namespace NHibernateMaps
{
  public class GuidStringGenerator : IIdentifierGenerator
  {
    public object Generate(ISessionImplementor session, object obj)
    {
      return new GuidCombGenerator().Generate(session, obj).ToString();
    }

  }
}

And in the mapping specify it as

 mapping.Id(x => x.Id)
    .Column("row_id")
    .CustomType(typeof(string))
    .GeneratedBy.Custom(typeof(GuidStringGenerator))
    .Length(36);
櫻之舞 2024-08-23 07:05:22

我不相信为主键使用不受支持的数据类型是否明智,但如果您确实需要这样做,那么您可以尝试编写一个 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.

鯉魚旗 2024-08-23 07:05:22

只需使用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.

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