简体   繁体   English

NHibernate与Composite-id实体多对一

[英]NHibernate many-to-one with a composite-id entity

How can I map a many-to-one relationship where the "one" entity has a composite-id? 如何映射“一个”实体具有复合ID的多对一关系? For example: 例如:

public class SingleEntity  
{  
  public int FirstId{get;set;}  
  public int SecondId{get;set;}  
  public string SomeData{get;set;}  
}  

The entity ManyEntity has (in addition to a primaryKey) columns for both SingleEntity 's FirstId and SecondId, so I want to be able to mape the relationship so that ManyEntity would look like that: 实体ManyEntity拥有(除了primaryKey之外) SingleEntity的FirstId和SecondId列,因此我希望能够ManyEntity关系,以便ManyEntity看起来像这样:

public class ManyEntity  
{  
public int Id{get;set;}  
public SingleEntity Single{get;set;}  
public string Name{get;set;}  
}  

How can I do it using NHibernate? 如何使用NHibernate做到这一点?

Fluent allows you to specify composite id mapping using CompositeId() method. Fluent允许您使用CompositeId()方法指定复合ID映射。 However, as we can read in this very method documentation: 但是,正如我们可以在此方法文档中阅读的那样:

Note: Prefer using a surrogate key over a composite key whenever possible. 注意:尽可能使用替代键而不是组合键。

Composite ids usually require lot of effort for discussable/no gains. 复合ID通常需要花费很多精力才能获得可讨论的/没有收益。 If you can , I would strongly advocate to use surrogate key. 如果可以的话 ,我强烈建议您使用代理密钥。 It's a lot easier. 这要容易得多。

Yet, if you're stuck with your DB schema, maybe those questions can help you: 但是,如果您坚持使用数据库模式,也许这些问题可以帮助您:

If you google those phrases (fluent nhibernate composite id), you'll quickly find out that awful lot of links somehow also contain word "problem" ... 如果您用谷歌搜索这些短语(流利的nhibernate复合ID),您将很快发现很多链接都包含“问题”一词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM