Vishal
Vishal

Reputation: 6368

getting an error in entity-framework using many to many relationship

I have two tables called Buttons and Tiles with many-to-many relationship as follows:

Buttons            *-----------
    ButtonID                  |
    Title                     |
                              |
Tiles              *-----------
    TileID
    Title

Now I am trying to query it something like :

b = new ObservableCollection<Buttons>(from x in db.Buttons
                                      where x.Tile == SelectedTileObj
                                      select x);

But I am getting an error as mentioned below:

Unable to create a constant value of type 'Data.Tiles'.
Only primitive types or enumeration types are supported in this context.

Upvotes: 0

Views: 33

Answers (1)

RePierre
RePierre

Reputation: 9566

Assuming TileID is a primary key, you could take another approach:

  • select the tiles that interest you (have the right id)
  • using the navigation property, get the buttons associated with the above tiles

    var tiles = db.Tiles.Where(t => t.TileId == SelectedTileObj.TileId);
    // t.Buttons should be the navigation property for retrieving associated buttons
    var buttons = tiles.SelectMany(t => t.Buttons);
    var b = new ObservableCollection<Buttons>(b);

Upvotes: 2

Related Questions