【问题标题】:Code First Entity Framework Asp.net MVC代码优先实体框架 Asp.net MVC
【发布时间】:2017-04-23 02:24:40
【问题描述】:

我有一个问题。我创建了一些模型来生成数据库,但有一个问题。当它生成数据库时,它引用两个 Album_id转一Audio_songs

专辑模型对我来说似乎是正确的,但我不确定。 如果你看到 Sql Query Generate by C# 它有 Album_idAlbums_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);

【问题讨论】:

  • 在您的音频歌曲模型中,您添加了两次专辑模型 {get;放;}。一个是Albums,一个是Category。这是导致问题的原因吗?
  • 是的,你有一个模型错误:public virtual Album Category { get; set; }
  • 大声笑,谢谢伙计们

标签: c# asp.net asp.net-mvc entity-framework ef-code-first


【解决方案1】:
       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; }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    相关资源
    最近更新 更多