views:

26

answers:

1

I have a Tags table and an Objects table.

A record of their relationships is kept via the ObjectTags table which has two columns. The columns store ObjectId and TagId (from the Tags and Objects tables), and both make up a composite key (can't have TagId and ObjectId twice).

In Entity Framework this table is not mapped as an object but rather enable "navigation" between the main tables. This is very cool, but how to i add to this table? What is the best way?

I add an Object and now i have it's ObjectId. I also add new Tags (reuse the already existing ones) and get their TagId. Now i should add the ObjectId and TagId to this relationship table... but how?

A: 

I had to use something like this:

db.Items.Where(id => id.Id == newItem.Id).First().Tags.Add(newTag);

This means. In the "db" in the "Items" table where "the Item Id is the one i'm looking for", select it, then go via the navigation property to the "Tags" table and add a "new Tag".

Doing this updates the relationship's ItemTags table with the new ids.

:)

basilmir