- In my mvc 4 project I have installed entity framework through nuget package manager
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.
server=.correct? Shouldn't you be using the server name instead of.? - kvothe