MySql + EF6 + .Net Core
2016-07-01 01:36
381 查看
2016年8月17日01:21:29 更新:这里介绍一下一个开源的 EF Core 的 MySQL 组件 [MySQL for .NET Core - Pomelo 扩展包系列][http://www.1234.sh/post/pomelo-data-mysql]
前请提要:http://www.cnblogs.com/JacZhu/p/5631075.html
在上面的那篇文章里面我成功的引用了 EF6 但是默认的数据库使用的是 SqlServer ,下面我会将 MySql 添加到项目中来。
先把下面的内容添加到 web.config 里面
注意,这里我把
还是在 Startup.cs 文件中的
同时我们需要更新 appsettings.json 文件
本来我以为弄完了上面的步骤就OK了,然而剧情就如往常一样,问题并没有解决,会报错说找不到 EntityFramework provider所以我只好在
网上说在 Web.Config 文件中做出如下修改也是可以的:
但是我试过了,发现没用
参考资料:
http://stackoverflow.com/questions/15142841/no-entity-framework-provider-found-for-mysql-data-mysqlclient-ado-net-provider
前请提要:http://www.cnblogs.com/JacZhu/p/5631075.html
在上面的那篇文章里面我成功的引用了 EF6 但是默认的数据库使用的是 SqlServer ,下面我会将 MySql 添加到项目中来。
准备 web.config 文件
讲道理 appsettings.json 这个文件应该是代替了 Web.config 的,然而在接下来的配置中还是需要这个文件。先把下面的内容添加到 web.config 里面
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.1.3.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <!-- Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380 --> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" /> </system.webServer> <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.8, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories> </system.data> <entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6"> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v13.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider> </providers> </entityFramework> </configuration>
注意,这里我把
ParkDbContext换成了
EFDbContext
重新注册DI
由于我们的连接字符串是在解析依赖性的时候注入的,所以DbProvider也需要我们使用同样的方法来注入依赖性
还是在 Startup.cs 文件中的
ConfigureService方法中添加绑定
services.AddScoped(sp => { var conn = DbProviderFactories.GetFactory(Configuration["ConnectionStrings:providerName"]).CreateConnection(); conn.ConnectionString = Configuration["ConnectionStrings:DefaultConnection"]; return new EFDbContext(conn); });
同时我们需要更新 appsettings.json 文件
{ "ConnectionStrings": { "DefaultConnection": "server=*******.com;user id=asaftp;password=********;persistsecurityinfo=True;database=asaftp", "providerName": "MySql.Data.MySqlClient" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } } }
本来我以为弄完了上面的步骤就OK了,然而剧情就如往常一样,问题并没有解决,会报错说找不到 EntityFramework provider所以我只好在
EFDbContext声明的地方加了个特性
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
网上说在 Web.Config 文件中做出如下修改也是可以的:
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
但是我试过了,发现没用
总结
估计很少会有人像我这么蛋疼的来写代码了,希望EF Core 能够尽快的支持 MySql ,这样 ASP.NET Core 完全跨平台就指日可待了!参考资料:
http://stackoverflow.com/questions/15142841/no-entity-framework-provider-found-for-mysql-data-mysqlclient-ado-net-provider
相关文章推荐
- MySql数据库基本操作练习
- MySQL5.7.13源码编译安装与配置
- mysql 数据库中 常用数据类型,约束
- MySQL 5.7 学习心得之安全相关特性
- MySQL 密码增强插件
- MySql5.6使用validate password 插件加强密码强度的安装及使用方法
- Mysql row number()排序函数的用法和注意
- MYSQL数据库及字段命名规范
- mysql 1064错误
- MySQL数据库PDO教程
- mysql用一个表的一列,更新另一表的一列
- SparkSQL JDBC连接 mysql
- 12步安装MySQL-Server5.5.32
- mysql从数据表中随机取数据!!!
- mysql中kill掉所有锁表的进程
- mysql5.7 社区zip版安装配置
- MySQL max_allowed_packet设置及问题
- mysql数据库开发中你遇到这些问题吗
- 详解MySql优化步骤
- mysql的sql分页函数limit使用