如何首先使用相互实现的类作为属性来编写代码?

时间:2014-06-15 10:23:50

标签: c# entity-framework model-view-controller ef-code-first

以下是我希望在拥有歌曲专辑的网站中拥有的2个课程,但我不确定如何使用种子方法。我已经就这个问题进行了非常广泛的阅读,但到目前为止我还没有发现这种情况。这可以通过不将Album作为属性轻松解决,因为我可以先播放歌曲,但我真的想要Album属性。事实上,如果我可以消除albumId并且只拥有Album属性,那也会很好,尽管我不确定如果没有albumId属性将如何强制执行外键关系。 我确信有一个简单的答案,但我很惊讶它很难找到一个例子。 所以这是我想要的2个课程:

public class Album
{
    public Album()
    {
        Songs = new List<Song>();
    }
    public int AlbumId { get; set; }
    public int ReleaseYear { get; set; }
    public int NumberOfTracks { get; set; }
    public string Artist { get; set; }
    public string Name { get; set; }
    public ICollection<Song> Songs { get; set; }
}

public class Song
{
    public int SongId { get; set; }
    public int TrackNumber { get; set; }
    public string Name { get; set; }
    public List<string> Composers { get; set; }
    public List<string> FeaturedArtists { get; set; }
    public List<string> Producers { get; set; }
    public Album Album { get; set; }
    public int? AlbumId { get; set; }
}

这是我不完整的种子方法:

protected override void Seed(IQsLab.Models.ApplicationDbContext context)
    {
        context.Albums.AddOrUpdate(
          p => p.Artist,
          new Album { 
              Artist = "Linguistics",
              Name = "The Writes of Passage",
              NumberOfTracks = 17,
              ReleaseYear = 2007,
              AlbumId = 1,
              Songs = new List<Song>()
              {
                  new Song{
                      TrackNumber = 1,
                      AlbumId = 1,
                      Album = ??????????????????????????????????????,
                      Name = "Glory",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      FeaturedArtists = new List<string>{
                          "Dj Solo",
                          "DJ Step 1"
                      }
                  },
                  new Song{
                      TrackNumber = 2,
                      Name = "Children of Atlantis",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      }
                  },
                  new Song{
                      TrackNumber = 3,
                      Name = "On the Grind",
                      Composers = new List<string>(){
                          "Dan Kemp"
                      },
                      Producers = new List<string>(){
                          "Dan Kemp"
                      }
                  },
                  new Song{
                      TrackNumber = 4,
                      Name = "No Turnin Back",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      FeaturedArtists = new List<string>{
                          "Tassa"
                      }
                  },
                  new Song{
                      TrackNumber = 5,
                      Name = "How Many",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      }
                  },
                  new Song{
                      TrackNumber = 6,
                      Name = "Have Faith",
                      Composers = new List<string>(){
                          "Dan Kemp"
                      },
                      Producers = new List<string>(){
                          "Dan Kemp"
                      }
                  },
                  new Song{
                      TrackNumber = 7,
                      Name = "Mozart's Finest",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      FeaturedArtists = new List<string>{
                          "Dj Solo",
                          "2Mex"
                      }
                  },
                  new Song{
                      TrackNumber = 8,
                      Name = "The Museum",
                      Composers = new List<string>(){
                          "Dan Kemp"
                      },
                      Producers = new List<string>(){
                          "Dan Kemp"
                      },
                      FeaturedArtists = new List<string>{
                          "DJ 3rdi"
                      }
                  },
                  new Song{
                      TrackNumber = 9,
                      Name = "Broken Tongues",
                      Composers = new List<string>(){
                          "Anno Domini"
                      },
                      Producers = new List<string>(){
                          "Anno Domini"
                      }
                  },
                  new Song{
                      TrackNumber = 10,
                      Name = "Politics",
                      Composers = new List<string>(){
                          "Kasper"
                      },
                      Producers = new List<string>(){
                          "Kasper"
                      }
                  },
                  new Song{
                      TrackNumber = 11,
                      Name = "The Music Is Ours",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      }
                  },
                  new Song{
                      TrackNumber = 12,
                      Name = "It's Us",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      FeaturedArtists = new List<string>{
                          "2Mex"
                      }
                  },
                  new Song{
                      TrackNumber = 13,
                      Name = "Where Did Hip-hop Go?",
                      Composers = new List<string>(){
                          "Dan Kemp"
                      },
                      Producers = new List<string>(){
                          "Dan Kemp"
                      }
                  },
                  new Song{
                      TrackNumber = 14,
                      Name = "Tha Realness",
                      Composers = new List<string>(){
                          "Kasper"
                      },
                      Producers = new List<string>(){
                          "Kasper"
                      }
                  },
                  new Song{
                      TrackNumber = 15,
                      Name = "How To Make It",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      }
                  },
                  new Song{
                      TrackNumber = 16,
                      Name = "A Warrior's Ballad",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      FeaturedArtists = new List<string>{
                          "Tassa"
                      }
                  },
                  new Song{
                      TrackNumber = 17,
                      Name = "Multiple Choice",
                      Composers = new List<string>(){
                          "Bobby Ruckuss"
                      },
                      Producers = new List<string>(){
                          "Bobby Ruckuss"
                      }
                  }
              }
          }
        );

    }

基本上,我如何设置Album = thisAlbumImCreatingRightNow,?

1 个答案:

答案 0 :(得分:0)

您只需创建相册和歌曲,设置它们之间的关系并调用保存更改。 DbContext将自动为您完成剩余的工作(更改跟踪/关系修正)。这就是ORM有趣的原因;

protected override void Seed(IQsLab.Models.ApplicationDbContext context)
{
   Album album1 = new Album { /* ... */ };
   Album album2 = new Album { /* ... */ };
   Album album3 = new Album { /* ... */ };

   Song1_1 = new Song { /* ... */ };
   Song1_2 = new Song { /* ... */ };
   Song1_3 = new Song { /* ... */ };

   // ...

   Album1.Songs.Add(Song1_1);
   Album1.Songs.Add(Song1_2);
   Album1.Songs.Add(Song1_3);

   // ...

   context.Albums.Add(Album1);
   context.Albums.Add(Album2);
   context.Albums.Add(Album3);

   // ...

   context.SaveChanges();

 }

注意1:context.Albums.Add(AlbumN)方法不仅将AlbumN附加到db上下文,还附加所有子项。他们都获得了州Added

注意2:当你致电SaveChanges时,EF会注意恢复实体的PK(例如Album1)并修复所有相关孩子的关系。在这种情况下,这意味着在插入Song1_*时将所有Album1的FK设置为​​已恢复的PK。

顺便说一句,除非您另外配置,否则默认的EF约定将在数据库中将int PK实现为标识,因此它们将自动生成。我假设你没有使用属性,Fluent API或自定义约定来调整你的模型。

所以,它比你想象的容易得多。