Hi folks,
Update
Checking StackOverflow OData service it's exactly like Posts / PostTags / Tags ... How can I replicate that, please?
So I've got a simple class called BlogPost - nothing special. Just a blog post. Now, I wish to add tagging for this class. Exactly like the tags for a StackOverflow question - 5 strings, space delimited. And then save this data to my Sql Server 2008 Database using Entity Framework 4.
So, at first I thought I could just add an ICollection<string> Tags { get; set; }
or a StringCollection Tags { get; set; }
and for the Database, just have a new Table called Tags
which is a foreign Key to the BlogPost and a VarChar(100) Tag
field.
But then I thought that if I do this, I'll just have lots of duplicated tags in that database table. Is it possible to have two tables, maybe.
- BlogPostsToTags
- Tags
Where the Tags are all unique instances.
Now that's easy to model in the DB, but I've got no idea how to do this in EF? Is this a good way of modelling it? Is that what is done here at SO?
I really like having a ICollection<string> Tags { get; set; }
(it's nice and clean) but i'm not sure how to have that AND have unique tags in the database using EF as my Data Access.
I just don't know how to add some tags for a blogpost and make sure the tags are unique but still reference back to the blogpost.
Like, should this be the responsibility of the database so when I try to save a blogpost, then at that point do i uniquly deal with tags .. by checking if they exist first and then either inserting or updating the unique tag .. and using this reference Id in the BlogPostsToTags table? What happens for race conditions? do I need to use transactions for this ... and if so .. what about read performance if the table gets locked?
Can anyone help, please?