【问题标题】:How to seed code first with classes that implement each other as properties?如何首先使用将彼此实现为属性的类来播种代码?
【发布时间】:2014-06-15 10:23:50
【问题描述】:

这是我希望在有歌曲专辑的网站中拥有的 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, ?

【问题讨论】:

  • EF 应该只插入一个AlbumId,这就是他所需要的。尝试查询 Songs 并检查 EF 是否填充了 Album 属性。
  • 谢谢,我知道这是一个解决方案,但这是我不喜欢的解决方案。如果这是唯一可用的解决方案,那就这样吧。我将删除 Album 属性或不在种子方法中设置它,但我希望有人有一种创造性的方式,我可以按照我想要的方式播种,并将 Album 属性作为类的一部分,因为它以后可能会派上用场。
  • 当具体化(从数据库中检索实体)时,EF 会在Album 放置一个正确的引用,以便您以后可以使用它
  • 嗯,好的,谢谢。我是 Code 的新手,所以我想我会停止尝试设置每首歌曲的 Album 属性,而只使用专辑 ID。谢谢。

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


【解决方案1】:

您只需创建专辑和歌曲,设置它们之间的关系并调用保存更改。 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_*的FK设置为​​插入Album1时恢复的PK。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    相关资源
    最近更新 更多