KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Okay, here's the scenario. I have 3 tables. One called aspnet_Users one called Categories and a linking table called User_Categories. aspnet_Users and Categories both have primary keys (UserId and ID respectively). The linking table has only two columns: CategoryID and UserId, and there are foreign key relationships setup for each column AND I have a unique key setup for the two columns on User_Categories. This sets up a many-to-many relationship between the aspnet_Users table and the Categories table. I generated my entities edmx file from this database setup, and everything looks perfect and works for almost all operations. What I want to do is add a new category from a form (which works perfecly by itself), and also, at the same time, associate a specific user with that newly submitted category. When I try to do this I get the error in my subject line. Here is the code I'm using to try this (ctx is my entities context object): public ActionResult Create(Category category, Guid userId) { aspnet_Users user = ctx.aspnet_Users.SingleOrDefault(x => x.UserId == userId); ctx.Categories.AddObject(category); user.Categories.Add(category); ctx.SaveChanges();; return RedirectToAction("Index"); } Why doesn't this work?
Tags (comma-separated)
Save Edits
Cancel