EntityFramework.SqlServer.dll 中发生类型为“System.Data.Entity.Core.EntityException”的异常,但未在用户代码中处理

Ale*_*nov 5 asp.net-mvc entity-framework entity-framework-4 asp.net-mvc-4 entity-framework-6

我是 ASP.NET MVC 的新手,我正面临这个异常,连接字符串看起来很完美,但仍然引发异常,如果有人告诉我为什么会发生,不胜感激。

谢谢你们

模型 1

namespace MVCTwice.Models
{
    public class StudentContext : DbContext
    {
        public DbSet<Student> studs { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

型号 2

namespace MVCTwice.Models
{
    [Table("tblStudents")]
    public class Student
    {
        public int id { get; set; }
        public string name { get; set; }
        public string gender { get; set; }
        public string totalMarks { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

动作方法

public ActionResult Index()
        {
            StudentContext studentContext = new StudentContext();
            //Student emp = studentContext.studs.Select(emp=>emp.)
           List<Student> emp=studentContext.studs.ToList();

            return View(emp);
        }
Run Code Online (Sandbox Code Playgroud)

看法

@model MVCTwice.Models.Student
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        @Model.gender
        @Model.name
        @Model.id
        @Model.totalMarks

    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

例外

在此处输入图片说明

连接字符串

 <connectionStrings >
    <add
         name="myConnectionString"
         connectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=LoginInfo;Data Source=.\SQLEXPRESS"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>
Run Code Online (Sandbox Code Playgroud)

小智 5

<connectionStrings>
  <add name ="StudentContext " 
        connectionString ="server=.; database=here your database name; integrated security=SSPI"
           providerName ="system.data.SqlClient"/>
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

这是您的代码,但请更改您的数据库名称,然后将其添加到 web.config 文件中。