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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40444962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:34:01  来源:igfitidea点击:

An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

asp.net-mvcentity-frameworkasp.net-mvc-4entity-framework-4entity-framework-6

提问by Alexander Zaldostanov

I am new to ASP.NET MVC, I am facing this exception, the connection string looks perfect but still, the exception is raised, appreciate if anyone give me why is happening.

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

Thank you guys

谢谢你们

Model 1

模型 1

namespace MVCTwice.Models
{
    public class StudentContext : DbContext
    {
        public DbSet<Student> studs { get; set; }
    }
}

Model 2

型号 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; }
    }
}

Action method

动作方法

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

            return View(emp);
        }

View

看法

@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>

Exception

例外

enter image description here

在此处输入图片说明

ConnectionString

连接字符串

 <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>

回答by user7778612

<connectionStrings>
  <add name ="StudentContext " 
        connectionString ="server=.; database=here your database name; integrated security=SSPI"
           providerName ="system.data.SqlClient"/>
</connectionStrings>

Here is your code but change your database name and then add it into the web.config file.

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

回答by Arshad

go to iis -> application pools -> find your application pool used in application -> click it and then click 'Advance Settings' in Actions panel. Find 'Identity' property and change it to localsystem.

转到 iis -> 应用程序池 -> 找到应用程序中使用的应用程序池 -> 单击它,然后在“操作”面板中单击“高级设置”。找到“身份”属性并将其更改为本地系统。

please run the project if it runs successfully then good if not may be your solution is in below links.

如果项目运行成功,请运行该项目,否则很好,如果没有,您的解决方案可能在下面的链接中。

After fixing the above step Some of you will experience another error saying "SQL Server Login error: Login failed for user 'NT AUTHORITY\SYSTEM'"

修复上述步骤后,有些人会遇到另一个错误,提示“SQL Server 登录错误:用户‘NT AUTHORITY\SYSTEM’登录失败”

So for this please follow below links: SQL Server Login error: Login failed for user 'NT AUTHORITY\SYSTEM'

因此,为此请遵循以下链接: SQL Server 登录错误:用户“NT AUTHORITY\SYSTEM”登录失败

https://dba.stackexchange.com/questions/47172/can-not-find-nt-authority-network-service-on-sql-server-2012

https://dba.stackexchange.com/questions/47172/can-not-find-nt-authority-network-service-on-sql-server-2012

回答by Satish

Here is the answer of this exception ---> Change your name of the connection string, The name must be same like your context class.

这是此异常的答案 ---> 更改连接字符串的名称,名称必须与上下文类相同。