ASP.NET 4 - How to retrieve data with MVC3 Many to Many relationship and EF4.1 Model First?

Up vote 1 down vote favorite share g+ share fb share tw.

I'm a very beginner and I want to do something really simple, I guess I missed something but I could not find the answer. I have two tables with many-to-many relationship mapped with EF4.1 public partial class Activity { public Activity() { this. Pack = new HashSet(); } public int ActivityId { get; set; } public string Type { get; set; } public string Title { get; set; } public string Price { get; set; } public virtual ICollection Pack { get; set; } } public partial class Pack { public Pack() { this.

Activity = new HashSet(); } public int PackId { get; set; } public string Name { get; set; } public string Description { get; set; } public string Price { get; set; } public virtual ICollection Activity { get; set; } } And a context class generated: public partial class EvgDatabaseEntities : DbContext { public EvgDatabaseEntities() : base("name=EvgDatabaseEntities") { } public DbSet PackSet { get; set; } public DbSet ActivitySet { get; set; } } In my index view, I just need to display datas from a Pack, and activities related to this pack. Here is the Controller : public ActionResult Index() { var packs = evgDB.PackSet. Include("Activity").ToList(); return View(packs); } Here is what I would like to do in my view: @model IEnumerable @{ ViewBag.

Title = "Index"; } @foreach (var pack in Model) { //SHOW PACK. ID PACK. DESCRIPTION //SHOW THE ACTIVITIES RELATED TO THIS PACK } What is the best way/practice to do this?

I could do it by creating a third table in my EDM etc., but what is the point to have the possibility to make a native many-to-many relationship so? Sorry if it seems obvious to you, it's not to me, i'm just a beginner. Thank you =) asp.

Net-mvc-3 razor entity-framework-4.1 model-first link|improve this question asked Feb 11 at 18:40user120413761.

Somehow you already have everything and everything looks correct. What is the question exactly? Especially I don't understand: "but what is the point to have the possibility to make a native many-to-many relationship so?

" – Slauma Feb 11 at 19:39 This all looks good to me, are you getting an error? – Paul Feb 11 at 23:40 Well, the main question is : How do I implement what I want to do in comment in the code : //SHOW PACK. ID PACK.

DESCRIPTION //SHOW THE ACTIVITIES RELATED TO THIS PACK Since I can't do anything like this (I know it doesn't make sense): @foreach (var pack in Model) { pack.activity. Activities } To show all the activites related to a pack. Should I write another foreach in the first one?

Thanks a lot for your answers. – user1204137 Feb 13 at 16:45.

I already have the third table in my database (I used the "generate database from model"). Thx for your answer. – user1204137 Feb 11 at 19:30.

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