Modeling a many-to-many relationship in ASP.NET MVC using LINQ to SQL?

The only solution I found was using PLINQ. It isn't perfect as far as I know though I use the "PLINQ method" manually: codeproject.com/KB/linq/linq-to-sql-many... I agree though, I hate writing all that code over and over again. It isn't a "clean" solution.

The only solution I found was using PLINQ. It isn't perfect as far as I know though. I use the "PLINQ method" manually: codeproject.com/KB/linq/linq-to-sql-many... I agree though, I hate writing all that code over and over again.It isn't a "clean" solution.

Thanks. I did come back to this question and re-read through the answers. I'm working on trying out this method and it seems promising.

More of what I was looking for. – Joe Philllips Jun 24 '10 at 5:33 I have changed my answer because I truly think this is the right way to go (plinq manually). It seems to be how many-to-many relationships really should behave.

– Joe Philllips Jun 27 '10 at 7:01.

I have many examples of this type of relationship in my current project. I'm using MVC 1 and LINQ-to-SQL. I went through exactly the same frustration then as you are experiencing now.

The biggest hurdle is accepting the fact that LINQ-to-SQL doesn't directly manage many-to-many relationships, but understanding that it doesn't need to in order to get the information you require. Let's start with the R in CRUD, since it's the easiest way to demonstrate what needs to be done. Now, at this point I would recommend creating a strongly-typed view model, just to ease the task of aggregating your view data and to simplify the assignment of the meal data to the Details view: public class MealDetailsViewModel { public int Id_Meal { get; set; } public string Title { get; set; } public string Description { get; set; } private List _Ingredients = new List(); public List Ingredients { get { return _Ingredients; } set { _Ingredients = value; } } } To retrieve a meal and its list of ingredients, here's the controller method: public ActionResult Details (int mealID) { Meal result = DataContext.

Meals . Where(a => a. Id_Meal == mealID) .SingleOrDefault(); MealDetailsViewModel viewModel = new MealDetailsViewModel { Id_Meal = result.Id, Title = result.

Title, Description = result. Description, Ingredients = result. Meals-Ingredients .

Where(a => a. Id_Meal == mealID) . Select(a => a.

Ingredient) .ToList() }; return View(viewModel); } As previously stated, LINQ-to-SQL doesn't directly support many-to-many relationships, which is why you cannot see the Ingredients entity directly from Meal. However, as illustrated in the controller action method, you can access the association entity (Meals-Ingredients) from the Meal entity. From the association entity, you can access the Ingredients entity to get the list of ingredients.

The view would look something like this: " %> Ingredients: | | | If your database schema is correctly set up with the foreign key relationships you require, this approach should give you the outcome you're looking for.

Thank you very much for taking the time to explain this! I will look at it in more detail later and try to implement it based on your suggestions. – Joe Philllips Jan 31 '10 at 21:12 I have made a few changes to your code to fix a few problems.

Still working on getting it totally implemented. – Joe Philllips Feb 1 '10 at 6:30 With a few minor adjustments, this did the trick – Joe Philllips Feb 2 '10 at 5:04 Glad to help...good luck moving forward. – Neil T.

Feb 2 '10 at 5:12.

I highly recommend Steve Sanderson's 'Pro ASP. NET MVC Framework'. It's by far the best ASP.NET MVC guide I've seen, and in my opinion much better than the Wrox book by the MSoft team.

Well worth the money if you want to get into it. Amazon Link.

1 for the book. It is the best one I have read on MVC as well. – Matt Spradley Jan 27 '10 at 7:16 I'm still trying to decide if I want to go as far as learn the innards of MVC.

Not ready to invest in a book quite yet – Joe Philllips Jan 27 '10 at 16:05 1 The book doesn't really explore 'the innards' just how to use it effectively. – UpTheCreek Jan 28 '10 at 6:41 If it's this complicated to do a basic many to many relationship, I'm not sure if I like mvc enough to use it effectively =P -- we'll see – Joe Philllips Jan 29 '10 at 3:53 1 Like most of you, I spent weeks trying to solve and resolve this exact issue and attributed my inability to solve the problem with a problem with MVC. The problem is not with MVC...finding the solution to this problem requires a better understanding of how LINQ-to-SQL does what it does.

Once you figure that out, implementing an MVC solution to handle the problem is pretty straightforward. – Neil T. Jan 31 '10 at 20:23.

You won't get everything on silver plate. You will have to go through hell of wtf yourself. It's like with me and Ruby on Rails - every time I see sample snippet or sample application - it seems super duper simple and neat.

But - because I've never actually created anything myself, I got no ideas with what even to start. So - just go through some books and sample applications. Then - create something.

This feeling (with what to start, how to structure etc. ) will disappear quite soon. Later on - don't forget about second extreme - feeling that you know everything you have to know. :) And yeah - you did not provide enough information.

There's various paths to go by. I believe - you won't want to hear advanced domain driven design approaches. Neither you want to see Sql right next to html markup.

Only you know 'the context' and can decide what's necessary, good and what's not.

This is how I feel. I'm trying to get through the "hell of wtf" part :) – Joe Philllips Jan 27 '10 at 14:41 1 Good answer. This is what worked for me too.

– cottsak Jan 30 '10 at 18:14 @cottsak thanks... – Arnis L. Jan 30 '10 at 18:38.

I suggest you check out Professional ASP. NET MVC 1.0. There is an entire free chapter available that walks through some of the setup for a real application.

And take a look at nerddinner.com/ to see the finished product. This is a very simple MVC application and if you get the book you can follow it from concept to finished product.

I did watch the creation of nerddinner – Joe Philllips Jan 27 '10 at 6:51 Alright, apparently the video is different than the PDF! The PDF is waaay more helpful. – Joe Philllips Jan 27 '10 at 17:28 I'm very confused.

The asker marks my answer up, and then marks it as the answer and now I'm marked down twice by other users... nice. It would be nice to know why. – Jeremy Seekamp Jan 28 '10 at 22:48 I read through nerddinner and it didn't explain how the RSVPs became a part of a Dinner.

I can't mark you up unless you edit your answer – Joe Philllips Jan 29 '10 at 0:42.

I find Steven Walther has some great tutorials for asp. Net MVC version 1, assuming you're working with the first version as version 2 is not released yet (RC currently). The first working tutorial starts here and you should be able to find the rest from there.

He also has lots of good tips if you search through his blog on that site. Also most of this tutorials cover VB and C#, if that's important to you.

C# is preferred but I am familiar with both – Joe Philllips Jan 27 '10 at 14:42.

So, I'm looking for a simple tutorial or explanation of what process you would go through to design a simple CRUD application that involves a many-to-many relationship, explained below. I may not have provided enough information so feel free to request updates. I would be interested in seeing a Linq To Sql solution.

I went through the nerddinner PDF and my model has to be a little different than his. I want a many-to-many relationship with my Meal and my Ingredients. He just uses a 1-to-many with his Dinner and RSVPs.

Also, he never shows how he attached the RSVPs to his Dinner model.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions