0

MVC4にクラスとクラスがありますPlaylistSong

ルール:

  • 曲は0個以上のプレイリストに含めることができます。
  • プレイリストには0曲以上を含めることができます。

だから私がしたことはこれを作成することでした:

public class Playlist
    {
        public int PlaylistID { get; set; }
        public string Name { get; set; }
        public DateTime Date { get; set; }

        public virtual IList<PlaylistSongs> PlaylistSongs { get; set; }
    }

    public class Song
    {
        public int SongID { get; set; }
        public string Title { get; set; }
        public string SongArtURL { get; set; }

        public virtual Artist Artist { get; set; }
        public virtual Genre Genre { get; set; }
        public IList<PlaylistSongs> PlaylistSongs { get; set; }
        public IList<Album> Albums { get; set; }
    }

    public class PlaylistSongs
    {
        public int PlaylistID { get; set; }
        public int SongID { get; set; }

        public virtual Playlist Playlist { get; set; }
        public virtual Song Song { get; set; }

        public int NumberVotes { get; set; }
    }

そして、私もそのようにオーバーライドOnModelCreatingしました:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PlaylistSongs>()
                          .HasKey(cp => new { cp.SongID, cp.PlaylistID });

            modelBuilder.Entity<Playlist>()
                        .HasMany(c => c.PlaylistSongs)
                        .WithRequired()
                        .HasForeignKey(cp => cp.SongID);

            modelBuilder.Entity<Song>()
                        .HasMany(p => p.PlaylistSongs)
                        .WithRequired()
                        .HasForeignKey(cp => cp.PlaylistID);
        }

しかし、問題。Song最初は何にも接続されていない(正常に機能する)を作成してPlaylistから、その曲をプレイリストに追加したいとします。プレイリストには曲のリストが含まれていませんが、代わりにプレイリストの曲のリストが含まれているので、どうすればよいですか?

私が欲しいのは:

  • 曲のリストを作成する(独立して)
  • その曲のリストをプレイリストに追加します
  • 次に、Code First Migrations Seedメソッドは、PlaylistSongsテーブルに必要な関連付けを自動的に作成します。
  • 次に、特定のプレイリストの曲に投票してもらうことができます。
  • フィールドNumberVotesはそれに応じて変更されます。

ありがとうございました。

4

1 に答える 1

1

代わりにデータ注釈を使用します。クラスにIdentityフィールドを追加しPlaylistSongます。

    public class PlayList
    {
        [Key]
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime Date { get; set; }

        public virtual IList<PlayListSong> PlaylistSongs { get; set; }
    }

    public class PlayListSong
    {
        [Key]
        public int ID { get; set; }
        public int PlayListID { get; set; }
        public int SongID { get; set; }

        public virtual PlayList Playlist { get; set; }
        public virtual Song Song { get; set; }

        public int NumberVotes { get; set; }
    }

    public class Song
    {
        [Key]
        public int ID { get; set; }
        public string Title { get; set; }
        public string SongArtURL { get; set; }

        //public virtual Artist Artist { get; set; }
        //public virtual Genre Genre { get; set; }
        public IList<PlayListSong> PlaylistSongs { get; set; }
       // public IList<Album> Albums { get; set; }
    }
于 2012-12-01T21:49:50.623 に答える