asp.net mvc 4 - EntityException was unhandled by the user code, Underlying provider failed an open -


  1. in mvc 4 project have installed entity framework through nuget package manager
  2. then added employeecontext.cs class file models folder added following code in

    public class employeecontext : dbcontext
    {
    public dbset employees {get; set;}
    }

and added namespace using system.data.entity;
3. added connection string in web.config file, in root directory:

<connectionstrings>   <add name="employeecontext"          connectionstring="server=.; database=sample; integrated security=sspi"         providername="system.data.sqlclient"/> </connectionstrings> 

4. mapped "employee" model class database table, tblemployee using "table" attribute shown below.

    [table("tblemployee")]       public class employee       {           public int employeeid { get; set; }           public string name { get; set; }           public string gender { get; set; }           public string city { get; set; }        }   

and added namespace "system.componentmodel.dataannotations.schema" table attribute.
5. created employeecontroller.cs in actionmethod details() shown below:

public actionresult details(int id)  {       employeecontext employeecontext = new employeecontext();       employee employee = employeecontext.employees.single(x => x.employeeid == id);        return view(employee);   }   

6. finally, following code in application_start() function, in global.asax file.
database.setinitializer<mvcdemo.models.employeecontext>(null);
, added namespace using system.data.entity;

when build project showing build succeeded , run program http://localhost/mvc4demo/employee/details/1
getting error in particular line saying

employee employee = employeecontext.employees.single(x => x.employeeid == id);      entityexception unhandled user code   underlying provider failed open 

when install entity framework nugget has latest version 6.0.2 doing wrong not able run project.

1.use exact name of server in connectionstring section. copy sql server login page. server details

<connectionstrings>   <add name="employeecontext"         connectionstring="server=laptop-j1iqqe3f\sqlexpress; database=sample; integrated security=true"         providername="system.data.sqlclient"/> </connectionstrings> 
  1. change code in "employee" model class following.

    using system;

    using system.collections.generic;

    using system.linq;

    using system.web;

    using system.componentmodel.dataannotations.schema; using system.componentmodel.dataannotations;

    namespace mvcdemo.models

    { [table("tblemployees")] /* map tablename our modelclass*/

      public class employeemodel       {         [key] //if property named other id,                //you need add [key] attribute it.               //otherwise give error "entity type has no key defined".         public int employeeid { get; set; }         public string name { get; set; }         public string gender { get; set; }         public string city { get; set; }     } } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -