发生了System.Data.Entity.Core.EntityException

时间:2017-08-26 23:14:09

标签: asp.net-mvc entity-framework

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

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

<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)atSystem.Web.Mvc.Async.AsyncResultWrapper.Wrap     pedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)    在

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

System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters&LT;&GT; c__DisplayClass46.b__3f()

内部例外1: SqlException:用户&#39; IIS APPPOOL \ DefaultAppPool&#39;登录失败。

我的控制器是

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

    }
}

请告诉我,因为我对这个概念不熟悉。

提前致谢

此致 Advit

2 个答案:

答案 0 :(得分:1)

请检查以下

  • 确保您能够从SQL连接到数据库 服务器管理系统(SSMS)。
  • 确保本地计算机上存在数据库。
  • 如果您无法连接到您的数据库。转到Windows服务和 检查服务是否正在运行。如果没有,请开始服务。
  • 如果一切正常。尝试使用Entity Framework连接到数据库 来自向导。它将为您设置一切。

由于

答案 1 :(得分:0)

我也遇到了同样的问题,不知道该怎么做。所以我只是将Project Properties中的设置从Local IIS更改为Visual Studio开发服务器/ IIS Server。它对我有用。