发生System.Data.Entity.Core.EntityException [英] System.Data.Entity.Core.EntityException occurred

查看:56
本文介绍了发生System.Data.Entity.Core.EntityException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力连接数据库.我的数据库不需要任何用户名和密码.

I am struggling to connect to DB. My db doesn't require any username and pwd.

我在Web.config中的连接字符串是

My connection string in Web.config is

<connectionStrings>
      <add name="EmployeeContext" 
           connectionString="Data Source=LAPTOP-
HKBMHNCA\SQL2016AGAIN;Initial Catalog=aspnet-CodeFirstWithMVC-
20170821025529;Integrated Security=True;" 
           providerName="System.Data.SqlClient" />  
 </connectionStrings>

错误日志如下

  System.Data.Entity.Core.EntityException occurred
  HResult=0x80131501
  Message=The underlying provider failed on Open.
  Source=EntityFramework
  StackTrace:
   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean 
   shouldMonitorTransactions)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T]
   (Func`1 func, IDbExecutionStrategy executionStrategy, Boolean 
   startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.
   <GetResults>b__5()
   at 
   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult]
   (Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 
   forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.
   <System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at MVCDemo.Controllers.EmployeeController.Details(Int32 id) in 
   C:\Users\homeuser\documents\visual studio 
   2017\Projects\MVCDemo\MVCDemo\Controllers\EmployeeController.cs:line 17
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext 
   controllerContext, IDictionary`2 parameters)
   at 
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext 
controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 
parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.
<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, 

ActionInvocationinnerInvokeState),位于System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)

ActionInvocationinnerInvokeState)atSystem.Web.Mvc.Async.AsyncResultWrapper.Wrap pedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) at

System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncR结果asyncResult)在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()

System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncR esult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters. b__3d() at

System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<> c__DisplayClass46.b__3f()

System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f()

内部异常1:SqlException:用户'IIS APPPOOL \ DefaultAppPool'登录失败.

Inner Exception 1: SqlException: Login failed for user 'IIS APPPOOL\DefaultAppPool'.

我的控制器是

namespace MVCDemo.Controllers
{
    public class EmployeeController : Controller
    {
    // GET: Employee
        public ActionResult Details(int id)
        {
            EmployeeContext employeeContext = new EmployeeContext();

            Employee employee =    employeeContext.Employees.Single(emp => emp.EmployeeId == id);

        return View(employee);
        }
    }
}

我的模型课是

namespace MVCDemo.Models
{
public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }



 }
}

namespace MVCDemo.Models
{
    public class EmployeeContext:DbContext
    {
        public DbSet<Employee> Employees { get; set; }

    }
}

请告知我,因为我是这个概念的新手.

Please advise me as I am new to this concept.

预先感谢

问候Advit

推荐答案

请检查以下内容

  • 确保您能够通过SQL连接到数据库服务器管理系统(SSMS).
  • 确保数据库在本地计算机上存在.
  • 如果无法连接到数据库.转到Windows服务,然后检查服务是否正在运行.如果没有,请启动服务.
  • 如果一切正常.尝试使用实体框架连接到数据库从向导.它将为您设置所有内容.

谢谢

这篇关于发生System.Data.Entity.Core.EntityException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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