如何使用 Fluent NHibernate 将复合 id 映射到复合用户类型?

发布于 2024-08-27 09:30:26 字数 1379 浏览 7 评论 0 原文

我正在使用一个遗留数据库愚蠢地设置一个索引,该索引由一个 char id 列和两个分别组成日期和时间的 char 列组成。我为日期和时间列创建了一个 ICompositeUserType,以映射到实体上的单个 .NET DateTime 属性,该属性在不属于 id 时自行工作。我需要以某种方式使用具有关键属性映射的复合 id,其中包括我的 ICompositeUserType 来映射到两个字符日期和时间列。显然,在我的 Fluent NHibernate 版本中,CompositeIdentityPart 没有 CustomTypeIs() 方法,所以我不能只在我的重写中执行以下操作:

mapping.UseCompositeId()
    .WithKeyProperty(x => x.Id, CommonDatabaseFieldNames.Id)
    .WithKeyProperty(x => x.FileCreationDateTime)
    .CustomTypeIs<FileCreationDateTimeType>();

类似于这甚至可以通过 NHibernate 实现,更不用说 Fluent 了?我没能找到任何关于此的信息。

更新:

更仔细地阅读 NHibernate 文档 后,我发现 NHibernate 确实支持 组件复合 ID ,但它看起来并不支持复合 ID 的自定义类型(即 IUserTypeICompositeUserType)。此外,我拥有的 Fluent 版本不支持 NHibernate 对组件复合 ID 的支持(请参阅 问题 387第 346 期)。我可能会尝试升级我的整个解决方案,看看它是否有效,如果有效的话,然后发布我自己的问题的答案...除非有人可以告诉我,而我不必这样做...8o)

i'm working w/ a legacy database is set-up stupidly with an index composed of a char id column and two char columns which make up a date and time, respectively. I have created a ICompositeUserTypefor the date and time columns to map to a single .NET DateTime property on my entity, which works by itself when not part of the id. i need to somehow use a composite id with key property mapping that includes my ICompositeUserType for mapping to the two char date and time columns. Apparently w/ my version of Fluent NHibernate, CompositeIdentityPart doesn't have a CustomTypeIs() method, so i can't just do the following in my override:

mapping.UseCompositeId()
    .WithKeyProperty(x => x.Id, CommonDatabaseFieldNames.Id)
    .WithKeyProperty(x => x.FileCreationDateTime)
    .CustomTypeIs<FileCreationDateTimeType>();

is something like this even possible w/ NHibernate let alone Fluent? I haven't been able to find anything on this.

UPDATE:

after reading the NHibernate documentation more carefully, i found out that NHibernate does support component composite IDs, but it doesn't look like it supports custom types (i.e. IUserType and ICompositeUserType) for composite IDs. Also, the version of Fluent i have doesn't have support for NHibernate's support of component composite IDs (see issue 387 and issue 346). i will probably try and upgrade my entire solution, see if it works, and post an answer to my own question if it does... unless someone can tell me w/o me having to do that... 8o)

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

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

发布评论

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

评论(2

挽心 2024-09-03 09:30:26

尝试添加这个

mapping.Map(x => x.FileCreationDateTime).Columns.Add("FileCreationDate", "FileCreationTime").CustomSqlType("YourType");

虽然我不确定 CustomSqlType 在一次引用两列时有什么意义。可能没有。您可以创建一个视图,将这些投影到正常日期时间的列吗?

Try adding this

mapping.Map(x => x.FileCreationDateTime).Columns.Add("FileCreationDate", "FileCreationTime").CustomSqlType("YourType");

Although I am not sure what sense CustomSqlType does when referring to two columns at once. Probably none. Can you create a view that projects these to columns to normal DateTime?

懷念過去 2024-09-03 09:30:26

NHibernate 不支持使用 IUserTypeICompositeUserTypes 来获取复合 ID。然而,它确实支持组件复合 ID,但 Fluent NHibernate 对此的支持有限。从我在源代码中看到的,它不允许您像 NHibernate 允许的那样指定组件属性的列名称。我现在不得不放弃,只创建两个 string 字段,数据库中的两个 string 日期字段可以单独映射到,然后有一个 DateTime ? 属性转换了两个 string 属性。支持 HeavyWave 的评论:“尽管如此,我认为您应该将每一列映射到其自己的字段,并提供组合它们的附加属性。”

mapping.CompositeId()
            .KeyProperty(x => x.Id, CommonDatabaseFieldNames.Id)
            .KeyProperty(x => x.FileCreationDateString, CommonDatabaseFieldNames.FileCreationDate)
            .KeyProperty(x => x.FileCreationTimeString, CommonDatabaseFieldNames.FileCreationTime);

NHibernate doesn't support the use of IUserType or ICompositeUserTypes for composite IDs. It does, however, support component composite IDs, but Fluent NHibernate has limited support for this. From what i could see in the source code, it doesn't allow you to specify the column names for the component properties like NHibernate allows. I had to give-up for now and just create two string fields that the two string date fields in the database could map to seperately, and then have a DateTime? property that translated the two string properties. Props to HeavyWave for their comment: "Although, I think you should just map each column to its own field and provide additional property that combines them."

mapping.CompositeId()
            .KeyProperty(x => x.Id, CommonDatabaseFieldNames.Id)
            .KeyProperty(x => x.FileCreationDateString, CommonDatabaseFieldNames.FileCreationDate)
            .KeyProperty(x => x.FileCreationTimeString, CommonDatabaseFieldNames.FileCreationTime);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文