最初にコードを使用するのではなく、ID と名前のフィールドを持つ artist というテーブル、id とタイトルのフィールドを持つ albums というテーブル、artist_id と album_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; }
}
MusicDBContext:
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);
}
}
ジャンクション テーブルが入力されていないため、これは機能しません。これがばかげた質問である場合は申し訳ありませんが、私は Entity Framework に非常に慣れていません。