0

I am trying to connect to a SQL Server database from my ASP.Net MVC 4 application.

The connection string I have is:

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

The error message I get is this:

The underlying provider failed on Open.

To try and fix this issue added this piece of code into my connection string in VB user id=js;password=jess1; but to no avail.

Another site said to add my main computer user to SQL and this didnt work either.

My model class:

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

My controller:

public ActionResult Details(int id)
{
    EmployeeContext employeeContext = new EmployeeContext();
    Employee employee = employeeContext.Employees.Single(x => x.EmployeeId == id);

    return View(employee);
}

EDIT

I also have this model in my application

namespace Department_Employee.Models
{
    [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; }
    }
}

Can anybody see where I have gone wrong?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TerrorTot38
  • 191
  • 2
  • 14
  • Did you use SqlConnection object to make sure you can reach db without EF? – ali Apr 25 '16 at 10:42
  • I am following a tutorial and this wasn't included. Can you explain where this should be and I will be happy to answer. – TerrorTot38 Apr 25 '16 at 10:54
  • 1
    This should help - http://stackoverflow.com/questions/2475008/mssql-error-the-underlying-provider-failed-on-open – Yogi Apr 25 '16 at 10:54
  • @Yogi I found the article before posting have tried but to no avial. – TerrorTot38 Apr 25 '16 at 11:00
  • Please check your SQL Server Login type, Mixed Mode or Windows Authentication, if your type Mixed Mod then set id and password from connectionstring. – rootturk Apr 25 '16 at 11:08
  • @TerrorTot38 By following [this link](http://www.dotnetperls.com/sqlconnection) you can make sure your Connectionstring is correct and database is running, Then, you can think about the EF problems. – ali Apr 25 '16 at 11:33

1 Answers1

0

you should try this,

<add name="EmployeeContext" connectionString="Data Source=DEVELOPER-PC; database=Sample; User ID=username;Password=password";MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" />
Sagar R
  • 595
  • 3
  • 14