1

How can I map a many-to-one relationship where the "one" entity has a composite-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:

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?

sternr
  • 6,216
  • 9
  • 39
  • 63

1 Answers1

0

Fluent allows you to specify composite id mapping using CompositeId() method. 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. 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"...

Community
  • 1
  • 1
k.m
  • 30,794
  • 10
  • 62
  • 86
  • The problem is the DB schema cannot be changed due to backward compatability with legacy software. I know SingleEntity can be mapped using composite-id, but how do I map the connection from the ManyEntity's side? – sternr Nov 25 '11 at 12:29