简体   繁体   中英

WP7 - Local DB - ManyToMany Linq2SQL

As the title indicates I am looking for the solution to this with the Association attribute also the property for the EntitySet<> setter since it differs for the Many To Many relationship I am not sure I am going in the right direction with since there are no examples of said relationship, due to L2S being very difficult with this type of relation.

Does the Foreign Key table have to have references or sets to the 2 tables that it joins...?

Example:

  • Hotels - Table
  • ScheduledFlights - Table
  • HotelToScheduledFlights - Table -- consists of HotelId & ScheduledFlightId both are FKs

Do I use a many to many in this case? or would I better off refactoring to a one to many?

If the foreign key relationships exists in SQL, L2S will have both the properties (ex HotelId, ScheduledFlightId) and the EntityRefs in the HotelToScheduledFlights table object. Hotels will then have an EntitySet pointing to HotelToScheduledFlights as will ScheduledFlights.

I think that this is a valid many to many relationship, so long as you are planning on needing to find hotels by flight and flights by hotel.

To get all of the flights tied to a hotel you would do:

Hotel h = dc.Hotels.First(); // Pick a hotel
List<ScheduledFlight> l = h.HotelToScheduledFlight.Select(i => i.ScheduledFlight).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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