NHibernate Mapping : How to map rows based on a field value
I have a [ContactNumbers]
table as defined below:
ID (PK) | PersonID (FK) | NumberType | Number
========|===============|============|=======
and a classes defined as:
public class Person
{
ContactNumber homePhone;
C开发者_如何学ContactNumber workPhone;
}
public class ContactNumber
{
string Number;
}
How would I define my HBM mapping/s for the Person
and ContactNumber
class so that Person.homePhone
is mapped to the corresponding row in the [ContactNumbers]
table with the FK observed, and [ContactNumbers].[NumberType]
equal to "HOME"
? ([NumberType]
is "WORK"
for Person.workPhone
.)
Already spent a good deal of the day just looking into this, and I couldn't find a solution just yet.
You cannot map single entity / instance to multiple rows and vice versa.
What you can do is do this:
class Person
{
public IList<ContactNumber> ContactNumbers { get; set; }
}
And then map the ContactNumbers class as a collection / ony-to-many association. The PersonID column is listed as a foriegn key so I assume there is a person table?
精彩评论