Code First实体框架Asp.net MVC
问题描述:
我有一个问题。我创建了一些模型来生成数据库,但有一个问题。当它产生数据库它参考两个 Album_id
到一个Audio_songs
Code First实体框架Asp.net MVC
专辑模型似乎对我来说,但我不确定。 如果你看到SQL查询用C#生成具有Album_id
和Albums_id
.ForeignKey("dbo.Albums", t => t.Albums_Id)
.ForeignKey("dbo.Albums", t => t.Category_Id)
- Album_id
- Albums_id
public class Album
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string About { get; set; }
public string Folder { get; set; }
public bool Approve { get; set; }
public string Picture { get; set; }
public System.DateTime CreateDate { get; set; }
public virtual ICollection<AudioSong> AudioSongs { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public bool IsHomePage { get; set; }
public bool Featured { get; set; }
}
public class AudioSong
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Url { get; set; }
public string Lyrics { get; set; }
public string Singer1 { get; set; }
public string Singer2 { get; set; }
public string Top10 { get; set; }
public string Top10no { get; set; }
public string Picture { get; set; }
public virtual Album Albums { get; set; }
public System.DateTime CreateDate { get; set; }
public virtual Lyric_writer Lyric_writers { get; set; }
public virtual ICollection<Album_comments> Album_comments { get; set; }
public virtual ICollection<Actor> Actors { get; set; }
public virtual Album Category { get; set; }
public bool IsHomePage { get; set; }
public bool Treading { get; set; }
public bool IsSlider { get; set; }
}
SQL Query C# generate
CreateTable(
"dbo.Albums",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false),
About = c.String(),
Folder = c.String(),
Approve = c.Boolean(nullable: false),
Picture = c.String(),
CreateDate = c.DateTime(nullable: false),
IsHomePage = c.Boolean(nullable: false),
Featured = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.AudioSongs",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false),
Url = c.String(),
Lyrics = c.String(),
Singer1 = c.String(),
Singer2 = c.String(),
Top10 = c.String(),
Top10no = c.String(),
Picture = c.String(),
CreateDate = c.DateTime(nullable: false),
IsHomePage = c.Boolean(nullable: false),
Treading = c.Boolean(nullable: false),
IsSlider = c.Boolean(nullable: false),
Albums_Id = c.Int(),
Category_Id = c.Int(),
Lyric_writers_Id = c.Int(),
Album_Id = c.Int(),
Tag_Id = c.Int(),
PlayList_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Albums", t => t.Albums_Id)
.ForeignKey("dbo.Albums", t => t.Category_Id)
.ForeignKey("dbo.Lyric_writer", t => t.Lyric_writers_Id)
.ForeignKey("dbo.Albums", t => t.Album_Id)
.ForeignKey("dbo.Tags", t => t.Tag_Id)
.ForeignKey("dbo.PlayLists", t => t.PlayList_Id)
.Index(t => t.Albums_Id)
.Index(t => t.Category_Id)
.Index(t => t.Lyric_writers_Id)
.Index(t => t.Album_Id)
.Index(t => t.Tag_Id)
.Index(t => t.PlayList_Id);
答
public class Album
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string About { get; set; }
public string Folder { get; set; }
public bool Approve { get; set; }
public string Picture { get; set; }
public System.DateTime CreateDate { get; set; }
public virtual ICollection<AudioSong> AudioSongs { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public bool IsHomePage { get; set; }
public bool Featured { get; set; }
}
public class AudioSong
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Url { get; set; }
public string Lyrics { get; set; }
public string Singer1 { get; set; }
public string Singer2 { get; set; }
public string Top10 { get; set; }
public string Top10no { get; set; }
public string Picture { get; set; }
public virtual Album Albums { get; set; }
public System.DateTime CreateDate { get; set; }
public virtual Lyric_writer Lyric_writers { get; set; }
public virtual ICollection<Album_comments> Album_comments { get; set; }
public virtual ICollection<Actor> Actors { get; set; }
public virtual Category Category { get; set; }
public bool IsHomePage { get; set; }
public bool Treading { get; set; }
public bool IsSlider { get; set; }
}
在您的音频乐曲模型,你已经添加了两个次相册样板{获得;组;}。一个拥有'相册',另一个拥有'类别'。这是造成这个问题吗? – Unbreakable
是的,你有一个模型错误:'公共虚拟专辑类别{get;组; }' –
大声笑我谢谢你们,谢谢你们 – Agha