C# 代码优先实体框架Asp.net MVC
我有个问题。我创建了一些模型来生成数据库,但有一个问题。当它生成数据库时,引用2C# 代码优先实体框架Asp.net MVC,c#,asp.net,asp.net-mvc,entity-framework,ef-code-first,C#,Asp.net,Asp.net Mvc,Entity Framework,Ef Code First,我有个问题。我创建了一些模型来生成数据库,但有一个问题。当它生成数据库时,引用2 Album\u idto oneAudio\u songs 专辑型号对我来说似乎很合适,但我不确定。 如果您看到由C生成的Sql查询,它具有Album\u id和Albums\u id .ForeignKey("dbo.Albums", t => t.Albums_Id) .ForeignKey("dbo.Albums", t => t.Category_Id)
Album\u id
to oneAudio\u songs
专辑型号对我来说似乎很合适,但我不确定。
如果您看到由C生成的Sql查询,它具有Album\u id
和Albums\u 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; }
}
公共类相册
{
[关键]
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
关于{get;set;}的公共字符串
公用字符串文件夹{get;set;}
公共bool Approve{get;set;}
公共字符串图片{get;set;}
public System.DateTime CreateDate{get;set;}
公共虚拟ICollection有声歌曲{get;set;}
公共虚拟ICollection类别{get;set;}
公共虚拟ICollection标记{get;set;}
公共bool IsHomePage{get;set;}
公共布尔函数{get;set;}
}
公共级有声歌曲
{
[关键]
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
公共字符串Url{get;set;}
公共字符串歌词{get;set;}
公共字符串Singer1{get;set;}
公共字符串2{get;set;}
公共字符串Top10{get;set;}
公共字符串Top10no{get;set;}
公共字符串图片{get;set;}
公共虚拟相册相册{get;set;}
public System.DateTime CreateDate{get;set;}
公共虚拟抒情作者抒情作者{get;set;}
公共虚拟ICollection相册\u注释{get;set;}
公共虚拟ICollection参与者{get;set;}
公共虚拟类别{get;set;}
公共bool IsHomePage{get;set;}
公共布尔运算{get;set;}
公共布尔IsSlider{get;set;}
}
在您的音频歌曲模型中,您添加了两次专辑模型{get;set;}。一个有相册
,一个有类别
。这是问题的原因吗?是的,您有一个模型错误:public virtual Album Category{get;set;}
lol ye my bad Thank guys
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; }
}