EntityFramework.SqlServer.dll中发生类型为'System.Data.Entity.Core.EntityException'的异常,但未在用户代码中处理 [英] An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

查看:410
本文介绍了EntityFramework.SqlServer.dll中发生类型为'System.Data.Entity.Core.EntityException'的异常,但未在用户代码中处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET MVC的新手,我面临着这个异常,连接字符串看起来很完美,但是仍然会引发异常,请感谢有人告诉我为什么会发生这种情况.

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.

谢谢你们

模型1

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

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

操作方法

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

            return View(emp);
        }

查看

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

例外

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>

推荐答案

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

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

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

这篇关于EntityFramework.SqlServer.dll中发生类型为'System.Data.Entity.Core.EntityException'的异常,但未在用户代码中处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆