使用EntityFramework6连接MySql数据库
准备工具:
VS2013、MySQL For VisualStudio 1.1.4、Connector/Net 6.8.3(百度网盘里)
程序包管理器执行命令:
Install-Package EntityFramework
Install-Package MySql.Data.Entity.EF6
Install-Package MySql.Data.Entity -Version 6.9.3
这块已经可以直接Install-Package MySql.Data.Entity -Version 6.9.3
用这个了
此时如果直接添加ado.net实体模型的话是有以下错误的:
注意了:App.config中生成的Provider是有问题的,这时候要手动添加红色线画出的一条:
注意了:下面的这块截图也可以不用了,因为你使用Install-Package MySql.Data.Entity -Version 6.9.3
可以自己生成了
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
这样最好先编译一下。
然后开始添加ADO.NET实体模型了
如果以上步骤还不行,就复制一下config代码过去做比较
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <configSections> 4 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 5 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 6 </configSections> 7 <startup> 8 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 9 </startup> 10 <entityFramework> 11 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 12 <parameters> 13 <parameter value="mssqllocaldb" /> 14 </parameters> 15 </defaultConnectionFactory> 16 <providers> 17 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 18 <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"> 19 </provider> 20 </providers> 21 </entityFramework> 22 <system.data> 23 <DbProviderFactories> 24 <remove invariant="MySql.Data.MySqlClient" /> 25 <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.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> 26 </DbProviderFactories> 27 </system.data> 28 <runtime> 29 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 30 <dependentAssembly> 31 <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" /> 32 <bindingRedirect oldVersion="0.0.0.0-6.9.5.0" newVersion="6.9.5.0" /> 33 </dependentAssembly> 34 </assemblyBinding> 35 </runtime> 36 <connectionStrings><add name="toastmaster" connectionString="server=localhost;user id=root;password=111111;persistsecurityinfo=True;database=toastmaster" providerName="MySql.Data.MySqlClient" /></connectionStrings></configuration>