I’m working on an asp.net-mvc project.
I have an Items table and a Tags table. There’s also an ItemTags table made up of two columns, setup as a composite key, storing Id’s from the first two tables.
Mapped to EntityFramework this latter table enables navigation between the tables.
When i add a new Tag for a specific Item i use:
db.Items.Where(id => id.Id == newItem.Id).First().Tags.Add(newTag);
My question is: What happens if the tag is already added in the Tags table, and i have another Item wanting to reuse the same tag?
What happens if i have a NEW Item that uses only tags that are already in the Tags table?
Does using:
db.Items.Where(id => id.Id == newItem.Id).First().Tags.Add(newTag);
AGAIN ensure i do not have the same Tag added twice. How do i “make” just the relationship between the Tag and Item, if they are both already in the tables.
Thank you!
So… Found the answer: (don’t know if there are any better ways of doing this …)
It’s the same Add() method that does both the “add if the item is new” and the “add if the item is already there”.