NHibernate两列同时作为复合键和外键

Mat*_*sca 3 c# nhibernate fluent-nhibernate

首先,我在网上和这里进行了彻底搜索,没有找到明确的手头任务解决方案.如果我的搜索不够准确且这个答案已经发布,我很抱歉.

问题:我有一张桌子.此表必须在两个字段上具有主键,并且其他字段包含一些数据.作为主键的两个字段也必须是外键,每个字段位于不同的表上.像(伪代码):

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

ProductCategory: (ProductId (fk on product), CategoryId (fk on category))(pk), SomeOtherField1, SomeOtherField2, etc
Run Code Online (Sandbox Code Playgroud)

现在我找不到如何使用Fluent Nhibernate配置它.虽然这个任务在EF Code First上是微不足道的,但是在这个项目上我被困在Net 3.5上并且不能使用它,所以NHibernate是唯一的另一个选择.

我尝试使用CompositeId(),References()和其他东西,尝试了各种组合,但没有一个工作.我不认为我得到的错误是相关的,我只需要一个像这样的场景的示例工作配置.

任何人都知道如何使用Fluent Nhibernate(没有xml配置)映射这个?

Col*_*e W 5

如果您的实体中有实际ProductCategory实体,ProductCategory那么您的映射应该看起来像这样(这里的关键KeyReference不是KeyProperty):

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

Map(x => x.SomeOtherField1);
Run Code Online (Sandbox Code Playgroud)

如果你只有你的Ids ProductCategory看起来像这样:

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

Map(x => x.SomeOtherField1);
Run Code Online (Sandbox Code Playgroud)

KeyReferences一个代码示例中的内容表示外键关系.