开发者

Many to Many Relationships |Insert | Delete

I started using Entity Framework (Using POCO Classes) for a MVC 3 C# project with Razor Views.

I am having trouble implimenting the Many to Many features with it

I am trying to Create a V开发者_StackOverflow社区iew that when a User is able to add phone numbers, emails, musictypes, flags, holds, ntoes, etc... through Many to Many relationships

i.e.) Company ----> CompanyVenue <---- Venue

I have managed to get EF storing the Venue and then being able to asign the Venue afterwards by selecting the Venue in a list and assigning to to the Company.

Is there a way to get The Venue to automatically store the relationship from the Company to the Venue with out having to select it though a list?

If that is not possible can anyone share a better way of Implimenting using EF.


Just by having this simple setup:

public Company
{
    public virtual ICollection<Venue> Venues { get; set; }
}

public Venue
{
    public virtual ICollection<Company> Companies { get; set; }
}

EF should be able to determine that you're declaring a many-to-many relation. I don't know if you're using EF 4.1 Code First or the model designer, but Code First should have enought with this to create the CompanyVenue table for you. And the model designer will define the "link" table for you as well as soon as you declare a many-to-many relation.

Note that the navigation properties are marked as virtual. This is necessary in the case of collections so that EF can make the proper updates in the related entities.


Here's how to manually do this in Code First. Firstly your POCO classes might look something like this:

public class Company
{
    public Company()
    {
        Venues = new List<Venue>();
    }
    public int CompanyID { get; set; }
    public string CompanyName { get; set; }
    public virtual ICollection<Venue> Venues { get; set; }
}

public class Venue
{
    public int VenueID { get; set; }
    public string VenueName { get; set; }
    public virtual ICollection<Company> Companies { get; set; }
}

In your DbContext:

public IDbSet<Company> Companies { get; set; }
public IDbSet<Venue> Venues { get; set; }

Also in the DbContext class override the OnModelCreating method. This is how you map the many to many relationship between Company and Venue:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Company>()
        .HasMany(comp => comp.Venues)
        .WithMany(venue => venue.Companies)
        .Map(mc =>
        {
        mc.ToTable("T_Company_Venue");
        mc.MapLeftKey("CompanyID");
        mc.MapRightKey("VenueID");
        }
    );

    base.OnModelCreating(modelBuilder);
}

I answered another EF many to many question with full sample code here:

MVC many to many

I also wrote the above into 2 blog posts with full solution and sample code available for download:

Part 1:

Saving many to many part 1

Part 2:

Saving many to many part 2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜