Advit Ramesh
Advit Ramesh

Reputation: 13

System.Data.Entity.Core.EntityException occurred

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

Upvotes: 0

Views: 3682

Answers (2)

Rkkhumba
Rkkhumba

Reputation: 1

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.

Upvotes: 0

Amaid Niazi
Amaid Niazi

Reputation: 162

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

Upvotes: 1

Related Questions