简体   繁体   中英

Error connecting to SQL database with Entity Framework from MVC .net application

I have the following error message: provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified

What I did was to create a model class, inheriting DbContext class:

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

and create another model class:

[Table("EmployeeList")]  
public class Employee
{
    public int EmployeeID { get; set; }            // no semi-colon
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string position { get; set; }
}

and in the controller class I coded:

public class EmployeeController : Controller
{
    public ActionResult EmployeeList(int id)
    {
        Employee employee = new Employee();         //instantiate the object of model class to be use
        EmployeeContext employeecontext = new EmployeeContext();   //create context model class  
        // assign the value of the context model object that is mapped to database                   
        employee = employeecontext.Employees.Single(x => x.EmployeeID == id);     
        return View("EmployeeDetail", employee);    // include view name, object of model class   
    }
}

Then I connect to the server and database using the option in Server Explorer. 2

I add a connection string associated with the DbContext class name that I created earlier:

<add name="EmployeeContext" 
     connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet-MVCDemo-20161213044601.mdf;Initial Catalog=aspnet-MVCDemo-20161213044601;Integrated Security=True;User Instance=True"
     providerName="System.Data.SqlClient" />

The server is checked to allow remote connect, is running from local database.

The website url is also type correctly: /localhost/{projectName}/Employee/EmployeeList/1

where 1 is the id parameter that I use for the action method EmployeeList in the Employee controller.

Your connection string is pointing to a SQL Express instance of SQL Server instead of the your actual database server. Data Source=.\\SQLEXPRESS

From your screenshot of your server explorer: 服务器浏览器

The Data Source component of your connection string should be Data Source=.\\sqlserver2014

Update : after reading your question again, I realised more than just your Data Source was wrong. Your connection string doesn't even point to the correct database "MVCDemo".

Full Connection String including correct Initial Catalog based on screenshot:

<add name="EmployeeContext" connectionString="Data Source=.\sqlserver2014;Initial Catalog=MVCDemo;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />

Note : you can also get the Server Explorer window to give you the connection string for the database being viewed, by right clicking on the database node and then on Modify Connection. Click Advanced and the connection string value will be displayed under the Data Source value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM