我用EF $ C C首先$并在NN关系的问题,假设我们有一个唱一些流派的歌手,所以我们需要这款车型:艺术家,流派和ArtistsGenres,我定义模型如下:
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:
这是我的艺人型号:
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; }
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);
}
}
但没有艺术家和流派在MVC自动生成视图之间无任何关系。
But there is not any relationship between Artists and Genres when MVC generate views automatically.
例如,我需要改变一个艺术家的流派在编辑看来,在创建视图我可以设置流派的艺术家,或在索引视图我想展示流派每个艺术家。但是,没有任何一代的有关艺术家的时候MVC自动生成的意见流派。
For example, I need to change Genres of an Artist in edit view, in Create view I can set Genres for an Artist, or in Index View I want show genres for each Artist. But there isn't any generation for Genres in relation to Artist when MVC generate views automatically.
我知道我可以从两侧同时访问的流派和艺术家,但我感兴趣的MVC中自动生成的观点,因为我们希望:为前:每个艺人演出相关的流派
I know I can access both Genres and Artists from both side but I am interesting in MVC automatically generate views as we want: for ex: for each artist show related Genres.
我怎样才能做到这一点?是我的模型是否正确?这是适用于任何(N至N)的关系,需要对ICollection的两侧?或者我需要的上下文类的 OnModelCreating压倒一切的一些元素的方法,例如是这样的:
How can I do this? Is my model correct? Is this true for any (n to n) relation that needs ICollection on both side? Or do I need some elements in overriding of OnModelCreating
method in context class, for example something like this:
modelBuilder.Entity<Artist>()
.HasMany(a => a.Genres)
.WithMany(g => g.Artists);
请帮帮我,我不知道NTON关系的真正实现。
Please help me, I don't know the exact implementation of NtoN relationship.
您还没有创建一个单独的型号
在一个多于─之间的关联模型一对多的关系。真正的 ArtistsGenres
是没有必要的。因此,删除它,你只需要改变你的模型构建器
来这样一句:
You haven't to create a separate Model
for association between to models in a many-to-many relationship. Really the ArtistsGenres
is not necessary. So, remove it, and you just have to change your modelBuilder
to this one:
modelBuilder.Entity<Artist>()
.HasMany(c => c.Genres)
.WithMany(x => x.Artists)
.Map(a => {
a.ToTable("ArtistsGenres");
a.MapLeftKey("ArtistId");
a.MapRightKey("GenreId");
});
这将使用 ArtistsGenres
表映射一个多到多艺术家之间的关系
表
流派
表自动。
It will use the ArtistsGenres
table to map a many-to-many relationship between Artists
table and
Genres
table automatically.
注意:当你定义的 ArtistsGenres
模式,EF不会把它视为一种关系,
因为你告诉他的嘿EF,我还有一个名为model ArtistsGenres
!请管理它给我!的
Note: When you define the ArtistsGenres
model, EF will not look at it as a relationship,
because you tell him that Hey EF, I have another model named ArtistsGenres
! Please manage it for me!!!
您的新实体和的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");
});
}
让我知道,如果您有任何疑问或需要任何部分澄清。
Let me know if you have any questions or need clarifications on any part.