我没有首先使用代码,我创建了一个名为艺术家的表,其中包含一个 ID 和名称字段,一个名为专辑的表,其中包含一个 ID 和标题字段,以及一个包含艺术家 ID 和专辑 ID 的 junction_artists_albums 表。
这是我的模型/上下文类:
艺术家:
public class Artist
{
public int Id { get; set; }
public string ArtistName { get; set; }
public ICollection<Album> albums { get; set; }
}
专辑:
public class Album
{
public int Id { get; set; }
public string AlbumName { get; set; }
public ICollection<Artist> artists { get; set; }
}
音乐数据库上下文:
public class MusicDBContext : DbContext
{
public MusicDBContext() { }
public DbSet<Artist> Artists { get; set; }
public DbSet<Album> Albums { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Album>().HasMany<Artist>(a => a.artists).WithMany(a => a.albums);
}
}
这不起作用,因为连接表没有被填写。对不起,如果这是一个愚蠢的问题,但我对实体框架很陌生