问题描述
昨天我知道 Entity Framework 是除了使用 Dataset 或 DataReader 访问数据库的另一种方法,然后我尝试让 Entity Framework 6 在 MVS 2013 中为我的 MySql 数据库服务器工作.
我用 .Net FrameWork 4.5.1 打开一个 WinForms.(所以我只有 App.config 但项目中没有 app/web 配置)在我安装 mysql-installer-community-5.7.3.0-m13.msi 和
通过安装EntityFramework包
<块引用>工具菜单 ->库包管理器 ->管理 NuGet 包解决方案...->在线->(搜索)EntityFramework(注意版本这个包的版本应该是6.0.2,如果不是,请点击更新->EntityFramework 更新)
当我尝试通过
添加 ADO.NET 实体数据模型时<块引用>右键项目->添加->新项目->ADO.NET 实体数据模型->从数据库生成 ->新连接->数据来源:->改变...->MySQL 数据库->使用服务器 IP、用户名和密码填写服务器名称->选择数据库名称->测试连接->好的
然后生成实体连接字符串->勾选将 App.Config 中的实体连接设置另存为 ->下一个>->
您要使用哪个版本的实体框架?有选项 Entity Framework 6.0 但你不能使用它,因为
<块引用>"您的项目引用了最新版本的实体框架;但是,与此兼容的实体框架数据库提供程序找不到您的数据连接的版本.退出此向导,安装兼容的提供程序,然后重新构建您的项目执行此操作".
如何解决?
顺便说一句,如果您在 Nuget 包中安装实体框架版本 5,那么您可能会在此处选择实体框架 5.0,您可能会成功使用实体框架 5 而不是版本 6.
首先,我们甚至不需要安装mysql-installer-社区-5.7.3.0-m13.msi.
- 安装最新的mysql-visualstudio-plugin
- 安装最新的mysql-connector-net
- 新的 C# .Net 4.5 框架 WinForms(对于 4.0 它应该基于 Entity Framework 6 是否支持 .NET 4.0?)
- 安装4个Nuget包(按照顺序,如果你在EntityFramework之前安装Mysql.Data.Entities,它将解决依赖并安装EntityFramework6.0.2但是我们需要什么是 EntityFramework 6.1.0)
<块引用>
实体框架
Mysql.Data
Mysql.Data.Entities
Mysql.Web
5.如果App.config中有标签entityFramework,请在标签启动后将其注释并在App.config中插入新标签entityFramework
<entityFramework><defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6"/><提供者><provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/></提供者></实体框架>
6.添加ADO.NET实体数据模型(如问题所述)
7.After Entity Connection string is generated(如问题所述)并在App.Config中勾选Save entity connection settings as然后单击Next
8.选择您的数据库对象和设置(表、视图或存储过程和函数)(没有您要使用哪个版本的实体框架?"因为我有只有一个 Entity Framework 6.0 提供者,如果我唯一的提供者有效,请直接跳过选择)
9.完成
恭喜^^
顺便说一句,您可能需要添加 .dll 文件
- MySql.Data.dll
- MySql.Data.Entity.EF6.dll
- MySql.Web.dll
在这个文件夹中
<块引用>C:Program FilesMySQLMySQL Connector Net 6.8.3Assembliesv4.5(32 位窗口)
C:Program Files (x86)MySQLMySQL Connector Net 6.8.3Assembliesv4.5(64 位窗口)
作为您的项目参考,以获取更多 EF6 功能.
Yesterday I knew that Entity Framework is another method to access database beside using Dataset or DataReader,then I tried to make Entity Framework 6 work for my MySql database server in MVS 2013.
I open a WinForms with .Net FrameWork 4.5.1. (so I only have App.config but no app/web config in the project) After I installed mysql-installer-community-5.7.3.0-m13.msi and
install EntityFramework package via
TOOLS menu -> Library Package Manager -> Manage NuGet Packages for Solution... -> Online -> (Search) EntityFramework (beware of version of this package and it should be version 6.0.2, if not then click Updates -> EntityFramework to update)
When I tried to add ADO.NET Entity Data Model via
Right click Project -> Add -> New Item -> ADO.NET Entity Data Model -> Generate from Database -> New Connection -> Data sources: -> Change...-> MySQL Database -> Fill in the Server name with server IP, Username and Password -> Choose the Database name-> Test Connection -> OK
Then Entity Connection string is generated -> Tick Save entity connection settings in App.Config as -> Next> ->
Which version of Entity Framework do you want to use? Have option Entity Framework 6.0 but you cannot use it because
"Your project references the latest version of Entity Framework; however, an Entity Framework database provider compatible with this version could not be found for your data connection. Exit this wizard, install a compatible provider, and rebuid your project before performing this action".
How to solve this?
By the way , if you install Entity Framework version 5 in Nuget Package then you might have option Entity Framework 5.0 here and you might success to use Entity Framework 5 but not the version 6.
First of all, we don't even need to install the mysql-installer-community-5.7.3.0-m13.msi.
- Install the latest mysql-visualstudio-plugin
- Install the latest mysql-connector-net
- New C# .Net 4.5 Framework WinForms (for 4.0 it should work based on Does Entity Framework 6 support .NET 4.0? )
- Install 4 Nuget Packages (follow sequence, if you install Mysql.Data.Entities before EntityFramework, it will resolve dependency and install EntityFramework 6.0.2 but what we need is EntityFramework 6.1.0)
EntityFramework
Mysql.Data
Mysql.Data.Entities
Mysql.Web
5.If you have tag entityFramework in App.config, please comment it and insert new tag entityFramework in App.config after tag startup
<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
6.Add ADO.NET Entity Data Model (as mentioned in question)
7.After Entity Connection string is generated (as mentioned in question) and Tick Save entity connection settings in App.Config as then click Next
8.Choose Your Database Object and Settings (Tables, Views, or Stored Procedures and Functions) (Don't have "Which version of Entity Framework do you want to use?" because I have only one Entity Framework 6.0 provider so direct skip the selection if my only provider is valid)
9.Finish
Congratulations ^^
By the way, you may need to add the .dll files
- MySql.Data.dll
- MySql.Data.Entity.EF6.dll
- MySql.Web.dll
inside this folder
C:Program FilesMySQLMySQL Connector Net 6.8.3Assembliesv4.5 (32bit windows)
C:Program Files (x86)MySQLMySQL Connector Net 6.8.3Assembliesv4.5 (64bit windows)
as your project reference for further EF6 functions.
这篇关于在 Microsoft Visual Studio 2013 的 WinForms 中启用 Entity Framework 6 for MySql (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!