1

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<Pack>();
    }

    public int ActivityId { get; set; }
    public string Type { get; set; }
    public string Title { get; set; }
    public string Price { get; set; }    
    public virtual ICollection<Pack> Pack { get; set; }
}

public partial class Pack
{
    public Pack()
    {
        this.Activity = new HashSet<Activity>();
    }

    public int PackId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Price { get; set; }

    public virtual ICollection<Activity> Activity { get; set; }
}

And a context class generated:

public partial class EvgDatabaseEntities : DbContext
{
    public EvgDatabaseEntities()
        : base("name=EvgDatabaseEntities")
    {
    }
    public DbSet<Pack> PackSet { get; set; }
    public DbSet<Activity> 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<myEvg.Models.EvgDatabaseEntities>
@{
    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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 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 '12 at 19:39
  • This all looks good to me, are you getting an error? – Paul Feb 11 '12 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 '12 at 16:45

1 Answers1

-1

Here is how to do it in Code First:

http://blogs.msdn.com/b/wriju/archive/2011/05/14/code-first-ef-4-1-building-many-to-many-relationship.aspx

You need to have the third table in the database otherwise where will this relationship be stored?

Chriseyre2000
  • 2,053
  • 1
  • 15
  • 28
  • I already have the third table in my database (I used the "generate database from model"). thx for your answer. – user1204137 Feb 11 '12 at 19:30