System.Data.Entity.Core.EntityException occurred

0

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

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>

The error log is as follows

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

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

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

My controller is

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

My model class is

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.

Thanks in advance

Regards Advit

asp.net-mvc
entity-framework
asked on Stack Overflow Aug 26, 2017 by Advit Ramesh • edited Aug 27, 2017 by (unknown user)

2 Answers

1

Please check the following

  • Make sure that you are able to connect to your Database from SQL Server Management System (SSMS).
  • Make sure the database exists on your local machine.
  • If you are not able to connect to to your database.Go to Windows services and check whether the service is running or not. If not please start the service.
  • If all is working fine. Try to connect to database using the Entity Framework from the wizard. It will setup everything for you.

Thanks

answered on Stack Overflow Aug 27, 2017 by Amaid Niazi
0

I also got the same problem and not sure what to do. So I just changed the settings in Project Properties from Local IIS to Visual studio development server/IIS Server. It worked for me.

answered on Stack Overflow Jun 2, 2018 by Rkkhumba

User contributions licensed under CC BY-SA 3.0