[英]Code First Entity Framework Asp.net MVC
我有一個問題。 我創建了一些模型來生成數據庫,但是有一個問題。 生成數據庫時,它將兩個Album_id
引用到一個Audio_songs
專輯模型對我來說似乎不錯,但我不確定。 如果看到由C#生成的Sql查詢,它具有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查詢C#生成
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; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.