Search code examples
c#silverlightentity-frameworkwcf-ria-services

Silverlight + WCF RIA + Child-Parent-ReferenceBook => How to fill ComboBox on UI side correctly


It could be the question about Entity Framework (WCF RIA domain model is based on it) but I'm not sure...

So I have Silverlight UI (MVVM) + WCF RIA domain services + Entity Framework on server side.

I have data Entities: Room -> Hotel (parent entity) <- RoomTypes plus Room has a reference to room type (so Room can be e.g. Single or Twin or Double etc). And one Hotel contains a set of Room Types used in this hotel.

Problem - on UI side I created user control - Room editor. There is I have text boxes (there is no any problem with text boxes and data binding) and also there is I have comboBox - to edit Room Type of Room. I can't figure out what is correct way to work this way... Please help me with it!

What I have now - I tried to load Hotels with Room types (in domain metadata - via [Include] attribute and plus this.ObjectContext.Hotels.Include("RoomTypes") in WCF RIA server side domain class) and in comboBox bind it to the room types list. But! If i choose some room type where was an exception - "Entity 'RoomType' cannot be attached to this EntityContainer because it is already attached to another EntityContainer."... Hotel is loaded by separate domain service and room types (in the User Control) is loaded by own domain service. How can I solve it?

Thanks in advance!


Solution

  • My second point in this post covers the issue you're running into.

    Link

    To correctly set up this association, you need to make sure you're loading Rooms and RoomTypes (specifically the RoomTypes you use in the ComoboBox) in the same DomainContext. Along those lines, you may also find my third point in that post helpful when dealing with asynchronously populating lookups.

    Kyle