花费了三天的时间,踩了无数坑,总算成功了一半
现在能够通过Code First模型生成数据库,但是Model First模型还是会闪退
<?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" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.8.8.0" newVersion="6.8.8.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
</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.8.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="connStr" connectionString="data source=127.0.0.1;user=root;password=root;database=sqltest;" providerName="MySql.Data.MySqlClient" />
<add name="Model1" connectionString="server=127.0.0.1;port=3306;user=root;password=root; database=Model1;sslmode=none;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
(吐槽一下,sql server才是亲儿子,一路顺畅走下来,啥bug也没有,也不需要下一堆插件)
下面一一介绍遇到介绍的坑和解决方案:
(1)第一个坑,要装插件,而且插件的版本还得对应,否则连不上数据库,我安装的版本如下
(2)第二个坑,版本匹配,版本不匹配会显示“System.ArgumentException: Option not supported…”
原因是版本问题,参考两篇文章解决:
https://zhuanlan.zhihu.com/p/43190042
https://www.cnblogs.com/tthjHiroki/p/8269480.html
大致意思是:Oracle 在升级了 MySql.Data 到 8.x 版之后,并没有直接将原先配套使用的 MySql.Data.Entity 同样升级到 8.x,而是改名为了 MySql.Data.EntityFramework,但是Nuget里的配置信息对不上,所以会出bug,解决方案是降低版本
我的版本,仅供参考:
①Install-Package EntityFramework -Version 6.1.3
②Install - Package MySql.Data - Version 6.8.8
③Install - Package MySql.Data.Entity - Version 6.8.3
(3)修改默认的:配置连接字符串(默认sqlserver,得改成mysql的)
(4)context模型前加
DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
不这么做就不会生成模型,也是一个坑,因为sql server没这样的问题
(5)mysql:调用 SSPI 失败,请参见内部异常。
解决方案:
连接字符串添加:
sslmode=none;
(6)提示“Authentication plugin ‘caching_sha2_password‘ cannot be loaded…”
只需要将my.ini 文件中的default_authentication_plugin属性设置成mysql_native_password即可
也就是 default_authentication_plugin = mysql_native_password
(7)找不到my.ini文件
我是解压安装的,这时要找到安装目录,自己建立my.ini服务,然后重启mysql服务
参考:https://blog.csdn.net/qq_43045747/article/details/125043557
不容易,步步是坑,还是老老实实用sql server吧