NHibernate的两列同时复合键和外键 [英] NHibernate two columns as composite key AND foreign keys at the same time

查看:473
本文介绍了NHibernate的两列同时复合键和外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经彻底搜查在线在这里,没有发现手头明确的解决方案的任务。我的道歉,如果我的搜索不够准确,这个答案已经发布

First of all, I've searched thoroughly online and here, without finding a clear solution to the task at hand. My apologies if my search wasn't accurate enough and this answer has already been posted.

问题:我有一个表。该表必须具有两个字段,和包含一些数据等领域的主键。两个场是主键还必须是外键,每一个不同的表。喜欢的东西(提前伪代码):

The issue: I have a table. This table must have a primary key on two fields, and other fields containing some data. The two fields that are primary key must also be foreign keys, each on a different table. Something like (pseudo code ahead):

Product: Id (pk)
Category: Id (pk)

ProductCategory: (ProductId (fk on product), CategoryId (fk on category))(pk), SomeOtherField1, SomeOtherField2, etc

现在我找不到如何使用功能NHibernate进行配置。这个任务是EF代码第一次微不足道的,在这个项目上我卡在.NET 3.5和不能使用,所以NHibernate的是唯一的其他选择。

Now I can't find how to configure this using Fluent Nhibernate. While this task is trivial on EF Code First, on this project I'm stuck on Net 3.5 and can't use that, so NHibernate was the only other choice.

我试着用CompositeId(),参考()和其他的东西,尝试不同的组合,但没有做的工作。我不认为我得到的错误是相关的,我只需要一个样本的工作配置是这样的情况。

I tried using CompositeId(), References() and other stuff, tried various combinations but none did work. I don't think the errors I got are relevant, I just need a sample working configuration for a scenario like this.

有谁知道如何映射这种使用功能NHibernate (无XML配置)?

Anyone does know how to map this using Fluent Nhibernate (no xml config)?

推荐答案

如果您有实际产品类别产品分类实体的实体,然后你的映射看起来应该像这样(这里的关键是 KeyReference 不是 KeyProperty ):

If you have actual Product and Category entities in your ProductCategory entity then your mapping should look something like this (the key here is KeyReference not KeyProperty):

CompositeId()
    .KeyReference(x => x.Product, "ProductId")
    .KeyReference(x => x.Category, "CategoryId");

Map(x => x.SomeOtherField1);

如果你只有IDS在产品分类它会是这个样子:

If you only have the Ids in your ProductCategory it would look something like this:

CompositeId()
    .KeyProperty(x => x.ProductId, "ProductId")
    .KeyProperty(x => x.CategoryId, "CategoryId");

Map(x => x.SomeOtherField1);



KeyReferences 1号代码示例说明外键关系。

The KeyReferences in the 1st code sample indicate the foreign key relationships.

这篇关于NHibernate的两列同时复合键和外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆