简体   繁体   English

从MVC .net应用程序使用Entity Framework连接到SQL数据库时出错

[英]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 我收到以下错误消息: 提供程序:SQL网络接口,错误:26-指定服务器/实例时出错

What I did was to create a model class, inheriting DbContext class: 我所做的是创建一个继承DbContext类的模型类:

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 2

I add a connection string associated with the DbContext class name that I created earlier: 我添加了一个与之前创建的DbContext类名称关联的连接字符串:

<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 网站的网址也输入正确:/ localhost / {projectName} / Employee / EmployeeList / 1

where 1 is the id parameter that I use for the action method EmployeeList in the Employee controller. 其中1是我用于Employee控制器中的操作方法EmployeeList的id参数。

Your connection string is pointing to a SQL Express instance of SQL Server instead of the your actual database server. 您的连接字符串指向SQL Server的SQL Express实例,而不是实际的数据库服务器。 Data Source=.\\SQLEXPRESS

From your screenshot of your server explorer: 从服务器浏览器的屏幕截图中: 服务器浏览器

The Data Source component of your connection string should be Data Source=.\\sqlserver2014 连接字符串的“数据源”组件应为“ 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". 您的连接字符串甚至都没有指向正确的数据库“ 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. 单击“高级”,连接字符串值将显示在“数据源”值下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到数据库时,ASP.NET MVC中的实体框架中发生错误 - Error occurring in Entity Framework in ASP.NET MVC while connecting to database 实体框架 MVC 应用程序是否可以在不同的数据库上运行 .sql 脚本 - Is it possible in Entity Framework MVC application to run .sql scripts on a different database Windows 应用程序部署与 SQL 服务器数据库和实体框架错误 - Windows application deployment with SQL Server database and Entity Framework error 如何使用实体框架 6 从 .Net 应用程序访问雪花数据库 - How to access Snowflake Database from .Net application using Entity Framework 6 ASP.NET MVC with ENTITY FRAMEWORK (SQL SERVER DATABASE) 图像未显示在视图中......错误显示“无法将“字节”转换为“字符串”” - ASP.NET MVC with ENTITY FRAMEWORK (SQL SERVER DATABASE) Image not showing in view ... The error says 'cannot convert "byte" to "string"' 实体框架7与.Net 5 MVC 6中的现有数据库 - Entity Framework 7 With Existing Database in .Net 5 MVC 6 无法使用实体框架连接到 C# asp mvc 应用程序中的 Sql 数据库,但相同的代码适用于 Winform 应用程序 - Can not connect to Sql database in C# asp mvc application with Entity Framework BUT the same code works with Winform application 从实体框架连接MySQL时出错 - Error when connecting MySQL from Entity Framework 通过.Net Core Console应用程序从Entity Framework Core 1.x连接到localdb - Connecting to localdb from Entity Framework Core 1.x via a .Net Core Console Application 使用实体框架将多个 sql 用户连接到一个数据库 - Connecting multiple sql users to one database with entity framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM