SQL错误实体框架代码第一方法

技术标签: 实体框架

我在代码第一种方法中连接到SQL数据库时遇到了麻烦。详细信息如下

<connectionStrings>
    <add name="MydbConn"
connectionString="Data Source=hostname\SQLEXPRESS;Initial Catalog=Test1;UserID=****;Password=****;Integrated Security=True"
providerName="System.Data.SqlClient"/>
  </connectionStrings>

下面是上下文助手

public class DBContext:DbContext
    {
        public DBContext():base("MydbConn")
        {
            Database.SetInitializer<DBContext>(new CreateDatabaseIfNotExists<DBContext>());
        }
        public DbSet<User> User { get; set; }
    }

用户模型

public partial class UserChatLog
    {
        [Key]
        public long UserLogId { get; set; }
        public string EnterpriseId { get; set; }
    }

最后dbhelper文件

public void Save(User user)
        {
            DBContext context = new DBContext();
            context.User.Add(User);
            context.SaveChangesAsync();
        }

我得到以下例外

System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist.

看答案

假设您的连接字符串很好,这可能是问题。我认为您的基本构造函数正在使用 ("MydbConn") 作为用于在基本构造函数中指定连接字符串的数据库名称,我相信这是正确的方式 base("name=MydbConn")


智能推荐

《A Unified Approach to Interpreting Model Predictions》论文解读——解释模型 预测的统一方法

作者:18届 cyl 日期:2021-08-09 论文:《A Unified Approach to Interpreting Model Predictions》 一、前言 在许多应用中,理解一个模型为什么要进行某种预测与预测的准确性同样重要。即确定每个输入特征的 重要性十分重要。 SHAP (SHapley Additive explanation,加性特征)则为每个输入特征分配了一个特定预测...

Java小白的第十七课一一方法传递&Math&Object

方法传递&Math&Object 回顾 1. 方法的参数传递 1.1 基本数据类型传值〖值传递〗 1.2 引用数据类型(数组、类)作为参数【引用/内存地址传递】 1.3 Sting字符串作为参数【值传递】 1.4 可变参数作为参数 1.4.1基本数型作为参数【值传递】 1.4.2 String 作为可变参数作为传递【值传递】 1.4.3 类作为可变参数【引用传递】 总结 2 Mat...

Orcad输出的第一方网表文件如何导入到Allegro软件中?

现在市面上用的最多的是,Orcad软件绘制原理图,Allegro软件绘制PCB版图。我们现在讲解一下怎么使用Orcad软件将绘制好的原理图,输出第一方的网表,然后将第一方的网表导入到Allegro软件中,具体操作如下: 第一步,选中原理图根目录,执行菜单命令Tools-Create Netlist,创建网表,如图6-26所示; 图6-26  使用Orcad软件创建第一方网表示意图 第二步...

Eclipse中创建一个简单的Maven项目(详细)

前提条件:Eclipse已经整合了Maven。 简单配置Maven 已经配置好的,请跳过 配置Maven的路径: window - preferences 找到Maven展开 点击ADD   在弹出的对话框中点击 Directory,选择Maven的路径,选择到Maven的根目录即可,不需要到bin目录!! 勾选新添加的Maven安装路径,点击Apply 配置Maven的仓库 ...

本地安装Nacos,启动时报错 Unable to start embedded Tomcat

本地安装Nacos,启动时报错 Unable to start embedded Tomcat org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerEx...

猜你喜欢

多条件查询+反向查询+分段区间数据查询+最后几行的数据+

VLOOKUP和MATCH函数 INDEX和MATCH函数 2.INDEX MATCH 反向查找 反向查找方法2: choose函数 =VLOOKUP(B2,CHOOSE({1,2},$E2:2:2:E9,9,9,D2:2:2:D$9),2,) 反向查找方法3: IF函数 =VLOOKUP(B2,if({1,2},$E2:2:2:E9,9,9,D2:2:2:D$9),2,) 3.分段区间的数据查询...

Mac安装MySQL的过程

1、先下载MySQL,下载地址:https://dev.mysql.com/downloads/file/?id=476460 如图选择下载: 2、下载后解压对应的文件mysql-8.0.11-macos10.13-x86_64.dmg,得到如下图所示的pkg包,双击下面的pkg包, 3、双击后显示下图,并点击“继续”按钮。 4、一路点击“继续”就可...

WorkFlow扩展篇Step.2—集合分组下的活动使用[下]-WF4.0

开篇         上篇我们主要是讲述了《WorkFlow扩展篇Step.1—集合分组下的活动使用[上]-WF4.0》如何使用集合Collection活动完成工作流的设计与开发,本篇我们将要继续 讲述集合活动内的另外二个活动的用法。 摘要 本篇我们是扩展篇的第二篇,本篇主要是讲述Collection的用法,一般来说...

使用webpack提升vue应用的4种方式

webpack是开发Vue单页应用必不可少的工具,它能管理复杂的构建步骤,并且优化你的应用大小和性能, 使你的开发工作流更加简单。 在这篇文章中,我将解释使用webpack提升你的Vue应用的4种方式,包括: 单文件组件 优化Vue构建过程 浏览器缓存管理 代码分离 关于vue-cli 如果你在使用 vue-cli 提供的模板来构建你的应用,那么webpack的相关配置已经提供好了,这些配置已经经...

关于——小程序如何拆分组件

啥也不说了直接上代码 第一步:先创建一个空文件夹(名字可以自定义) 第二步:在上一步创建的空文件夹里面在创建一个空文件夹 第三步:在里面这个空文件夹上面右键点击会出来这个白色框框找到里面的 新建component 点击 第四步: 新建component 点击后会出来一下四个文件(一定要包裹在第二次创建的空文件夹里面) 第五步: 到这就创建好了接下来引入组件 第六步:子组件在js中通过propert...

问答精选

C++ NetBeans - Removing Console from Win32

I have a Win32 app that displays a console window in the back. How can I, using NetBeans/C++, remove this console window? Thanks in advance. you might want to go for "Right-Mouse-Button: Properti...

Node losing gravity after SCNAction

I'd like to drop an object and then move it back to the top and let it fall again. The first part is working, but then the node seems to lose its gravity and isn't falling again. It looks like its phy...

PyQt5 store time of keyPressEvent

Issue: I have a program where I will be showing several pages with a stacked widget, and users will have to press a button (using code I've written below) to go to the next page of the stacked widget....

How to initialize a systemc port name which is an array?

I wanted to initialize a port name. The port is an array and my code does not work. The code below would work by giving clk with a name "clk". However clk port is not an array: How do I name...

Java source code can not be found in eclipse

In an xpages application a javav source code was added to the Local folder within the Lotus nsf file. Now can not be seen, and can not be found with search. The code still woking, but it is not possib...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答