0
votes
  1. In my mvc 4 project I have installed entity framework through nuget package manager
  2. Then added EmployeeContext.cs class file to the models folder then added the following code in to that

    public class EmployeeContext : DbContext
    {
    public DbSet Employees {get; set;}
    }

and I added namespace using System.Data.Entity;
3. Now I added a connection string in to the web.config file, in the root directory:

<connectionStrings>
  <add name="EmployeeContext" 
        connectionString="server=.; database=Sample; integrated security=SSPI"
        providerName="System.Data.SqlClient"/>
</connectionStrings>

4. Mapped "Employee" model class to the database table, tblEmployee using "Table" attribute as 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 the namespace "System.ComponentModel.DataAnnotations.Schema" for the table attribute.
5. I created EmployeeController.cs in which the ActionMethod Details() is 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, the following code in Application_Start() function, in Global.asax file.
Database.SetInitializer<MVCDemo.Models.EmployeeContext>(null);
and added the namespace using System.Data.Entity;

when I build this project it is showing build succeeded and run this program http://localhost/MVC4Demo/Employee/Details/1
I am getting error in this particular line saying that

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

EntityException was unhandled by the user code  
Underlying provider failed an open

When I install the entity framework from nugget it has the latest version 6.0.2 what is that I am doing wrong so that I am not able to run this project.

1
Make sure the connectionString is correct. - kvothe
upto my knowledge whatever I presented in my code that connectionString only I have used. - kool kims
Is server=. correct? Shouldn't you be using the server name instead of .? - kvothe
Actually server=. is used to local server only no. - kool kims
actually I have checked the sql server it is not in the local I have changed the server=QUTED004/SQLEXPRESS actual in which my db is there. - kool kims

1 Answers

0
votes
  1. Use exact name of your Server in ConnectionString section. Copy it from 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>
    
  2. Change code in "Employee" Model class as 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")]  /* to map tablename with our Modelclass*/
    
    
          public class EmployeeModel
    
    
            {
                [Key] //If the property is named something other than Id, 
                      //you need to add the [Key] attribute to 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; }
            }
        }