How does the Model n—n relationship in EF Code First work with automatically produced views?

.net c# ef-code-first entity-framework

Question

I use EF Code First and have a problem in n-n relationship, assume we have a singer that sing in some genres, so we need this models: Artist, Genre, and ArtistsGenres, I define Models as following:

My Artist Model Is This:

public class Artist
{
    public long Id { get; set; }
    public string Name { get; set; }
    public ICollection<Genre> Genres { get; set; }
}

My genre model is:

public class Genre
{
    public long Id { get; set; }
    public string Title { get; set; }
    public ICollection<Artist> Artists { get; set; }
}

Moreover, my context class

public class MusicDB : DbContex
{
    public DbSet<Artist> Artists { get; set; }
    public DbSet<Genre> Genres { get; set; }
    public DbSet<ArtistsGenres> ArtistsGenres { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<Artist>()
            .HasMany(a => a.Genres)
            .WithMany(g => g.Artists)
            .Map(model => {
                model.ToTable("ArtistsGenres");
                model.MapLeftKey("Artist_Id");
                model.MapRightKey("Genre_Id");
            });

        base.OnModelCreating(modelBuilder);
        }
}

But when MVC build views automatically, there is no connection between Artists and Genres.

For instance, I can establish a musician's genre in the create view, update a musician's genre in the edit view, or display each musician's genre in the index view. However, when MVC produce views automatically, there is no creation for Genres in respect to Artist.

I am aware that I have access to both Artists and Genres from either side, but what interests me is how MVC may automatically construct views as needed, such as those that display relevant Genres for each Artist.

How can I go about this? Will my model work? Do all (n to n) relations that need ICollection on both sides fall under this description? Or do I need to add certain components to overrideOnModelCreating method inside the context class, such as this:

modelBuilder.Entity<Artist>()
    .HasMany(a => a.Genres)
    .WithMany(g => g.Artists);

Please assist me; I am unsure of how the NtoN connection should be implemented precisely.

1
10
11/22/2011 2:53:09 PM

Popular Answer

You don't need to make a different one.Model for association in a many-to-many link between two models. really theArtistsGenres is not required. Therefore, take it out and just alter yourmodelBuilder relating to this

modelBuilder.Entity<Artist>()
    .HasMany(c => c.Genres)
    .WithMany(x => x.Artists)
    .Map(a => {
        a.ToTable("ArtistsGenres");
        a.MapLeftKey("ArtistId");
        a.MapRightKey("GenreId");
    });

It'll employ theArtistsGenres table that depicts a many-to-many connectionArtists desk andGenres automatically a table.

Note: When you establish theArtistsGenres model, EF won't see it as a connection because you've informed him that Hello EF, I have a new model that'sArtistsGenres ! Please take care of things for me!

Your new dbcontext and entities will be as follows:

public class Artist {
    public long Id { get; set; }
    public string Name { get; set; }
    public ICollection<Genre> Genres { get; set; }
}

public class Genre {
    public long Id { get; set; }
    public string Title { get; set; }
    public ICollection<Artist> Artists { get; set; }
}

public class MusicDB : DbContex {

    public DbSet<Artist> Artists { get; set; }
    public DbSet<Genre> Genres { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    modelBuilder.Entity<Artist>()
        .HasMany(c => c.Genres)
        .WithMany(x => x.Artists)
        .Map(a => {
            a.ToTable("ArtistsGenres");
            a.MapLeftKey("ArtistId");
            a.MapRightKey("GenreId");
        });

}

Please let me know if there are any parts that are unclear or if you have any queries.

13
11/22/2011 1:33:58 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow