EF代码中的模型n - n关系如何自动生成视图正常工作?

Sae*_*eid 10 .net c# entity-framework ef-code-first

我使用EF Code First并且在nn关系中存在问题,假设我们有一个歌手在某些类型中唱歌,所以我们需要这个模型:Artist,Genre和ArtistsGenres,我将模型定义如下:

这是我的艺术家模型:

public class Artist
{
    public long Id { get; set; }
    public string Name { get; set; }
    public ICollection<Genre> Genres { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和我的类型模型:

public class Genre
{
    public long Id { get; set; }
    public string Title { get; set; }
    public ICollection<Artist> Artists { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的上下文类:

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);
        }
}
Run Code Online (Sandbox Code Playgroud)

但是当MVC自动生成视图时,Artists和Genres之间没有任何关系.

例如,我需要在编辑视图中更改艺术家的类型,在创建视图中我可以为艺术家设置流派,或者在索引视图中我想要为每个艺术家显示流派.但是当MVC自动生成视图时,与Artist相关的Genres没有任何代.

我知道我可以从两边访问流派和艺术家,但我很有兴趣MVC自动生成我们想要的视图:例如:每个艺术家节目相关的流派.

我怎样才能做到这一点?我的模特是否正确?这对于任何需要ICollection的(n到n)关系都是如此吗?或者我OnModelCreating在上下文类中覆盖方法时需要一些元素,例如:

modelBuilder.Entity<Artist>()
    .HasMany(a => a.Genres)
    .WithMany(g => g.Artists);
Run Code Online (Sandbox Code Playgroud)

请帮帮我,我不知道NtoN关系的确切实现.

jav*_*iry 13

您不必为Model多对多关系中的模型之间的关联创建单独的关联.真的ArtistsGenres没有必要.所以,删除它,你只需要改变你modelBuilder的这个:

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

它将使用该ArtistsGenres表自动映射Artists表和 Genres表之间的多对多关系.

注意:当您定义ArtistsGenres模型时,EF不会将其视为关系,因为您告诉他Hey EF,我有另一个名为的模型ArtistsGenres!请为我管理!!!

您的新实体和dbcontext将是这些:

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");
        });

}
Run Code Online (Sandbox Code Playgroud)

如果您对任何部分有任何疑问或需要澄清,请与我们联系.


Oed*_*dum 0

艺术家和流派之间的联系是艺术家流派。

所以艺术家包含:ID、姓名

类型包含:ID、标题

而ArtistsGenre包含:ID艺术家、ID流派