views:

38

answers:

1

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?

A: 

Sounds like you are on the right track. In your tag table you might want to consider having a proper cased and a normalized (all lower case, no punctuation) version of the tag so you can map similar versions onto the same tag (e.g. StackOverflow, stackoverflow, stack-overflow).

A many-to-many association between that and your blog posts will give you a collection of Tags on any entry.

You can do the uniqueness code in C#: use Hashset<Tag> and the various set operators to find the new tags. You can add the new tags one by one with exception handling and appropriate retry code for optimistic concurrency exceptions. Once you have all the tags in the database you can add any new associations you need to the blog post and save those changes too.

Where it gets hard is when you want to apply these same tags to lots of other tables (unless they all inherit from some common base entity table in which case it's easy).

alt text

Hightechrider
@Hightechrider cheers for the reply :) I've never heard of the `Hashset<T>` object but did a quick search and saw it came in .NET 3.5 - nice :) So yeah, that helps with unique tags for my POCO object but I still don't know how to do this in EF? Especially when I could have a `Tag` Entity <- not sure how to do a custom Save for a BlogPost so when it tries to save the tags, it retrieves all the existing ones and inserts any new ones (if that makes sense). Do I need to use a special stored proc for that, on the tag insert/update states? Also, these tags are only for one table .. not multiple.
Pure.Krome