【发布时间】: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