entityframework使用CodeFirst创建MySql数据库出错的解决方法恢复
先告诉大家一个秘密,EF在使用 update-database 时候,使用的连接字符串来自于解决方案中的“启动项目”,而不是你在包管理器中选择的“默认项目”
0x01. 先说错误,方便大家检索到
开发环境,VS2015+MySql+EntityFramework6
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
<package id="MySql.Data" version="6.9.7" targetFramework="net45" />
<package id="MySql.Data.Entity" version="6.9.7" targetFramework="net45" />
</packages>
执行数据库的时候报错:
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.Data.SqlClient.SqlException (0x80131904): 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)
这时候我的配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="conn.mysql" providerName="MySql.Data.MySqlClient" connectionString="Data Source=127.0.0.1;port=3306;Initial Catalog=moa;user id=root;password=;" />
</connectionStrings>
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
</configuration>
0x02,一头雾水,各种怀疑
一个项目从一个解放方案,移动到另一个,就出错,BeyongCompare 比较除了应该不一样的都一样,我都怀疑EF有一个小数据库,针对每个项目记录什么配置了。
0x03,柳暗花明,发现疑点
当执行这个命令:
PM> Enable-Migrations -ConnectionStringName "conn.mysql" -Force -Verbose
Using StartUp project 'Masap.Service.Contract'. Using NuGet project 'Moa.Data'. Checking if the context targets an existing database... System.InvalidOperationException: No connection string named 'conn.mysql' could be found in the application config file.
看到红色字了吧, 我的EF项目是Moa.Data,和Masap.Service.Contract 没有关系,在结合找不到 "conn.mysql" 连接字符串,我顿悟了。马上吧解决方案中的启动项目设置为“Moa.Data",再次运行:
PM> Enable-Migrations -ConnectionStringName "conn.mysql" -Force -Verbose
Using StartUp project 'Moa.Data'.
Using NuGet project 'Moa.Data'.
Checking if the context targets an existing database...
Code First Migrations enabled for project Moa.Data.
PM> Add-Migration Init
Scaffolding migration 'Init'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration Init' again.
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201509301418201_Init].
Applying explicit migration: 201509301418201_Init.
Running Seed method.
PM>
一切正常了。
0x04,总结
EF在使用包管理器执行命令的时候,所有的配置都使用解决方案中”启动项目“的配置文件(app.config or web.config)。而不是EF所在项目的配置文件。
entityframework使用CodeFirst创建MySql数据库出错的解决方法恢复的更多相关文章
- django中mysql数据库设置错误解决方法
刚在django中settings.py进行设置mysql数据库. 当进行执行python manage.py shell命令时会报以下错误: 只需要在settings.py中 DATABASES = ...
- Centos6.8忘记MySQL数据库root密码解决方法
一.更改MySQL配置文件my.cnf,跳过密码验证. 编辑配置文件/etc/my.cnf文件,在[mysqld]下面添加skip-grant-tables,保存退出.如图: vim /etc/my. ...
- IDEA(2018)连接MySQL数据库失败的解决方法(报错08001)
解决方法: 将url改成: jdbc:mysql://localhost:3306/studentmanage?useSSL=true&serverTimezone=Hongkong& ...
- Linux 删除mysql数据库失败的解决方法
使用命令:drop database xxx:删除本数据库时却删除失败,系统提示出现了错误,错误代码为: ERROR 1010 (HY000): Error dropping database(can ...
- sqlserver数据库出错的解决方法
1.SQLServer2008数据库sa账户登录127.0.0.1失败 http://blog.sina.com.cn/s/blog_546a30270102weh2.html 2.SqlServer ...
- 忘记本地MySQL数据库密码的解决方法
平台:win7 1.打开cmd窗口,进入 MySQL的安装目录. 2.停止MySQL的服务.已经停止了. 右键MySQL,看到启动是可点击的,证明MySQL服务已停止运行. 3.将root用户对数据库 ...
- mysql数据库表卡死解决方法
---恢复内容开始--- 问题引起原因: 由于在执行大量插入操作的时候意外终止程序之后, MySQl的线程并没有被终止,导致表不能打开和操作 - 解决思路就是找到等待的线程并kill -- 查看所有 ...
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...
- 使用EF CodeFirst连接MySql数据库
如何使用EF CodeFirst连接MySql数据库? 我们这篇文章介绍怎么使用EF连接MySql 作者的环境 VS2017.Win10.MySql5.x 前言 一般在EF中,默认是使用SqlServ ...
随机推荐
- collection of vim vim tutorial for beginner
在linux命令行下编辑文档 http://blog.csdn.net/niushuai666/article/details/7275406 http://linuxconfig.org/vim-t ...
- 快速上手seajs——简单易用Seajs
快速上手seajs——简单易用Seajs 原文 http://www.cnblogs.com/xjchenhao/p/4021775.html 主题 SeaJS 简易手册 http://yslo ...
- 单片机与控制实验(5)——重量测量并在LCD12864显示
一.实验目的和要求 掌握点阵式液晶显示屏的原理和控制方法,掌握点阵字符的显示方法.掌握模拟/数字(A/D)转换方式,进一步掌握使用C51语言编写程序的方法,使用C51语言编写实现重量测量的功能. 二. ...
- Dictionary Learning(字典学习、稀疏表示以及其他)
第一部分 字典学习以及稀疏表示的概要 字典学习(Dictionary Learning)和稀疏表示(Sparse Representation)在学术界的正式称谓应该是稀疏字典学习(Sparse Di ...
- js实现简单的图片轮播
js代码如下 <script type="text/javascript"> var n=1; var map=new Array(); map[0]=new Imag ...
- #研发解决方案#iDB-数据库自动化运维平台
郑昀 创建于2015/12/2 最后更新于2015/12/2 关键词:数据库,MySQL,自动化运维,AutoDDL,刷库,帐号授权,审核,回滚 提纲: 数据库自动化运维什么?别人家是怎么玩的? 我们 ...
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- iOS判断是模拟器还是真机
#if TARGET_IPHONE_SIMULATOR //模拟器 #elif TARGET_OS_IPHONE //真机 #endif
- [转]Android ListView 与 RecyclerView 对比浅析—缓存机制
从源码角度剖析ListView 与 RecyclerView 缓存机制的不同 https://zhuanlan.zhihu.com/p/23339185 原文地址:http://dev.qq.com/ ...
- Xamarin的不归路-ios模拟器调整窗口大小
ios模拟器调整窗口大小: