Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC4 EF5“;无效列";错误_C#_Asp.net_Entity Framework_Asp.net Mvc 4 - Fatal编程技术网

C# ASP.NET MVC4 EF5“;无效列";错误

C# ASP.NET MVC4 EF5“;无效列";错误,c#,asp.net,entity-framework,asp.net-mvc-4,C#,Asp.net,Entity Framework,Asp.net Mvc 4,我是一个初学者,我正在写一个ASP.NETMVC4项目。首先,我创建了一个数据库,然后将其连接到项目,编写了类,现在我遇到了一些问题。问题是我无法从数据库中挖掘任何内容,因为“无效的列名'Song\u SongID'”。 这些课程看起来是这样的: [Table("Users")] public class User { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public i

我是一个初学者,我正在写一个ASP.NETMVC4项目。首先,我创建了一个数据库,然后将其连接到项目,编写了类,现在我遇到了一些问题。问题是我无法从数据库中挖掘任何内容,因为
“无效的列名'Song\u SongID'”。

这些课程看起来是这样的:

[Table("Users")]
public class User
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public System.DateTime DateOfBirth { get; set; }
    public string Email { get; set; }
    public string AboutYourself { get; set; }
    public string Guitar { get; set; }
    public string Country { get; set; }
    public string ProfilePic { get; set; }

    public virtual ICollection<Song> Songs { get; set; }
    public virtual ICollection<Song> UserLikes { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

[Table("Songs")]
public class Song
{
    public int SongID { get; set; }
    public string Name { get; set; }
    public string Genre { get; set; }
    public string File { get; set; }
    public System.DateTime DateOfPost { get; set; }
    public string Lyrics { get; set; }
    public short Likes { get; set; }
    public int UserUserID { get; set; }

    public virtual User User { get; set; }
    public virtual ICollection<User> UsersWhoLiked { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

[Table("Comments")]
public class Comment
{
    public int CommentID { get; set; }

    public int UserUserID { get; set; }
    public int SongSongID { get; set; }

    public virtual Song Song { get; set; }
    public virtual User User { get; set; }
}
此代码:

using System;
using System.Linq;
using System.Data.Entity;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Data.Objects.SqlClient;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace testef {       
    [Table("Users")]
    public class User {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public System.DateTime DateOfBirth { get; set; }
        public string Email { get; set; }
        public string AboutYourself { get; set; }
        public string Guitar { get; set; }
        public string Country { get; set; }
        public string ProfilePic { get; set; }

        public virtual ICollection<Song> Songs { get; set; }
        public virtual ICollection<Song> UserLikes { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }

    [Table("Songs")]
    public class Song {
        public int SongID { get; set; }
        public string Name { get; set; }
        public string Genre { get; set; }
        public string File { get; set; }
        public System.DateTime DateOfPost { get; set; }
        public string Lyrics { get; set; }
        public short Likes { get; set; }
        public int UserUserID { get; set; }

        public virtual User User { get; set; }
        public virtual ICollection<User> UsersWhoLiked { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }

    [Table("Comments")]
    public class Comment {
        public int CommentID { get; set; }

        public int UserUserID { get; set; }
        public int SongSongID { get; set; }

        public virtual Song Song { get; set; }
        public virtual User User { get; set; }
    }

    public class TestEFContext : DbContext {
        public DbSet<User> Users { get; set; }
        public DbSet<Song> Songs { get; set; }
        public DbSet<Comment> Comments { get; set; }

        public TestEFContext(String cs)
            : base(cs) {
            Database.SetInitializer<TestEFContext>(new DropCreateDatabaseAlways<TestEFContext>());

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Comment>().HasRequired(x => x.Song).WithMany(y => y.Comments).WillCascadeOnDelete(false);
            modelBuilder.Entity<Comment>().HasRequired(x => x.User).WithMany(y => y.Comments).WillCascadeOnDelete(false);
        }
    }

    class Program {
        static void Main(string[] args) {
            String cs = @"Data Source=ALIASTVALK;Initial Catalog=TestEF;Integrated Security=True; MultipleActiveResultSets=True";
            using (TestEFContext ctx = new TestEFContext(cs)) {                 
                Console.WriteLine("The value is " + ctx.Users.Count().ToString());
            }
        }
    }
}

使用上述FK。

是否为此使用迁移?这是POCO方法?不是。我使用模型设计器创建了一个数据库模型(我有.edmx文件),然后生成了DB。之后,我创建了一个新项目,将其与创建的数据库连接,并完成了授权和注册。那很好。现在我需要从数据库中输入数据,或者添加数据,我已经描述了一个问题。您什么时候收到这个错误?@aritra当我试图通过以下行从数据库中输出
User
实体时:
DB.Users.Find(WebSecurity.CurrentUserId)
其中
DB
是上下文。您是否尝试过在Song\u SongID上按住shift+ctrl+F组合键以查找。在你的项目中?非常感谢!!!我已经更改了我的数据库,将这些行添加到我的
用户表中:
[Song_SongID]INT NULL
约束[FK_SongID]外键([Song_SongID])引用[dbo]。[SongID]([SongID])
在[dbo]。[Users]([SongID]ASC]上创建非聚集索引[IX FK_Songs_SongID]现在可以正常工作了!我不知道我是否做得对,但如果有一些问题我无法找到解决方案,我会写在这里。再次感谢你!嗯,这很好。。。但请记住,这会修复错误消息。我必须承认,类和导航属性对我来说似乎有点复杂。是的,我已经考虑过了。看来将来导航属性会出现一些问题。但无论如何,将由他们决定:)
using System;
using System.Linq;
using System.Data.Entity;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Data.Objects.SqlClient;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace testef {       
    [Table("Users")]
    public class User {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public System.DateTime DateOfBirth { get; set; }
        public string Email { get; set; }
        public string AboutYourself { get; set; }
        public string Guitar { get; set; }
        public string Country { get; set; }
        public string ProfilePic { get; set; }

        public virtual ICollection<Song> Songs { get; set; }
        public virtual ICollection<Song> UserLikes { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }

    [Table("Songs")]
    public class Song {
        public int SongID { get; set; }
        public string Name { get; set; }
        public string Genre { get; set; }
        public string File { get; set; }
        public System.DateTime DateOfPost { get; set; }
        public string Lyrics { get; set; }
        public short Likes { get; set; }
        public int UserUserID { get; set; }

        public virtual User User { get; set; }
        public virtual ICollection<User> UsersWhoLiked { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }
    }

    [Table("Comments")]
    public class Comment {
        public int CommentID { get; set; }

        public int UserUserID { get; set; }
        public int SongSongID { get; set; }

        public virtual Song Song { get; set; }
        public virtual User User { get; set; }
    }

    public class TestEFContext : DbContext {
        public DbSet<User> Users { get; set; }
        public DbSet<Song> Songs { get; set; }
        public DbSet<Comment> Comments { get; set; }

        public TestEFContext(String cs)
            : base(cs) {
            Database.SetInitializer<TestEFContext>(new DropCreateDatabaseAlways<TestEFContext>());

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Comment>().HasRequired(x => x.Song).WithMany(y => y.Comments).WillCascadeOnDelete(false);
            modelBuilder.Entity<Comment>().HasRequired(x => x.User).WithMany(y => y.Comments).WillCascadeOnDelete(false);
        }
    }

    class Program {
        static void Main(string[] args) {
            String cs = @"Data Source=ALIASTVALK;Initial Catalog=TestEF;Integrated Security=True; MultipleActiveResultSets=True";
            using (TestEFContext ctx = new TestEFContext(cs)) {                 
                Console.WriteLine("The value is " + ctx.Users.Count().ToString());
            }
        }
    }
}
USE [TestEF]
GO

/****** Object:  Table [dbo].[Users]    Script Date: 03/12/2013 15:07:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Users](
    [UserID] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](max) NULL,
    [LastName] [nvarchar](max) NULL,
    [DateOfBirth] [datetime] NOT NULL,
    [Email] [nvarchar](max) NULL,
    [AboutYourself] [nvarchar](max) NULL,
    [Guitar] [nvarchar](max) NULL,
    [Country] [nvarchar](max) NULL,
    [ProfilePic] [nvarchar](max) NULL,
    [Song_SongID] [int] NULL,
 CONSTRAINT [PK_dbo.Users] PRIMARY KEY CLUSTERED 
(
    [UserID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[Users]  WITH CHECK ADD  CONSTRAINT [FK_dbo.Users_dbo.Songs_Song_SongID] FOREIGN KEY([Song_SongID])
REFERENCES [dbo].[Songs] ([SongID])
GO

ALTER TABLE [dbo].[Users] CHECK CONSTRAINT [FK_dbo.Users_dbo.Songs_Song_SongID]
GO