NHibernate: Many-to-many relationship with field in the relationship table

孤街醉人 提交于 2019-12-17 16:29:20

问题


I'm scratching my head; I have a Car table and a Customer table that have a many-to-many relationship. In this relationship table I want to add a column that can tell me what kind of relationship this is; is the customer testdriving the car, do he want to buy the car, ect. What I want to end up with is a class Car object that holds a collection of Customers and the relationship information. I might be looking at this the wrong way so feel free to push me in the right direction.


回答1:


Make the relationship an entity:

class CarRelation
{
  Car Car {get; set; }
  RelationType Type {get; set;}
}

A pure many-to-many relation doesn't have any additional properties.




回答2:


I think that you are missing an additional entity. You need to add an entity that expresses the customer's interest in the car. You will need to find the right name to fit your business domain, but here is my guess:

You have CUSTOMER table to store information about a specific customer. CUSTOMER has a one to many relationship to the CUSTOMERINTEREST table. CUSTOMERINTEREST stores information about the kinds of shopping activities the customer is engaged in (test drives, window shopping, etc.). CUSTOMERINTEREST has a many to one relationship with the CAR table. CAR stores information about specific automobiles in the store's inventory.

So, my best guess is that if the thing in the middle of your relationship needs more columns than just the two foreign keys then you don't really have a many to many relationship. You have two many to one relationships and you just haven't identified the thing in the middle yet.



来源:https://stackoverflow.com/questions/2713329/nhibernate-many-to-many-relationship-with-field-in-the-relationship-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!