views:

191

answers:

0

Possible Duplicate:
Modeling a many-to-many relationship in ASP.NET MVC using LINQ to SQL

Hi there,

The short version of this question is "Is there a way to gracefully handle database insertion for an object that has a many-to-many field that has been set up in a partial class?" Apologies if it's been asked before.

Example
Suppose I have a typical MVC setup with the tables:

  • Posts {PostID, ...}
  • Categories {CategoryID, ...}

A post can have more than one category, and a category can identify more than one post. Thus suppose further that I need an extra table:

  • PostCategories {PostID, CategoryID, ...}

This handles the many-to-many relationship between posts and categories. As far as I know, there's no way to do this in Linq-to-SQL right now so I have to shoehorn it in by adding a partial Post class to the project to add that functionality. Something like:

public partial class Post
{
   public IEnumerable<Category> Categories{ get { ... } set { ... } }
}

So I can now create a "Create" view that automatically populates a "Categories" UI item. It even handles like any other field- fun!

This is where the trouble starts.

So here's my question:
How do you get automatic object model binding to work cleanly with an object that has a many-to-many relationship to control? The workaround that makes many-to-many relationships possible relies on the Post object having a PostID in order to be associated with CategoryID(s), which is only issued after the Post object has been submitted for validation and insertion.

Bit of a Catch22 here. Any terminology, links, or tips you can provide would be tremendously helpful!