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 ...
随机推荐
- eclipse的package, folder, source folder 异同以及相互转化
1 相同点:都是文件夹; 不同点: 我们用面对对象思维来看; 首先说folder, 三者的父类(object),就是普通的文件夹,它和我们window下面使用的文件夹没有任何区别; source fo ...
- echarts中显示效果option中必有的属性
写一个最简单的效果让option中不可缺少的属性. var option = { xAxis:[ //x轴,数组对象,其下至少有一个对象 {.....} ], yAxis:[//y轴,数组对象,其下可 ...
- JS(去掉前后空格或去掉所有空格)的用法
1. 去掉字符串前后所有空格: 代码如下: function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } 说明 ...
- windows下mysql客户端输入中文显示??解决方法
>>>>>>>>>>>>>>>>>>>> 1.检查并修改mysql的my.ini ...
- 1.4 jQuery方法,JSON介绍
jQuery方法: jQuery添加元素: append()方法: $("元素").append("追加内容"); prepend()方法: $("元 ...
- 360浏览器导出Excel闪退BUG
最近这半个月在疯狂的修改各种BUG,所以比较少更新博客. 现在谈谈这个360浏览器导出Excel的BUG的解决方法. 该BUG常出现在win7系统与xp系统导出Excel的瞬间关闭导出弹窗. 目前互联 ...
- URAL 2089 Experienced coach Twosat
Description Misha trains several ACM teams at the university. He is an experienced coach, and he doe ...
- PB12.5.2安装
一.从12.5升级到12.5.2_build5550安装步骤: 1.安装VS2010及SP1 2.安装12.50(可以只装PB)DV68538-65-1250-01.zip 3.安装EBF20963 ...
- xsl: normalize-space(string str) 函数
本文出自http://technet.microsoft.com/zh-cn/magazine/ms256063%28VS.90%29.aspx 通过去掉前导和尾随空白并使用单个空格替换一系列空白字符 ...
- Cucumber(一): Preparation
Every time I wrote some code in ruby and executed our cucumber features I craved for something simil ...