本文介绍了在 Microsoft Visual Studio 2013 的 WinForms 中为 MySql (C#) 启用实体框架 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我知道 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 Package 中安装了 Entity Framework 版本 5,那么您可能会在此处选择 Entity Framework 5.0,并且您可能会成功使用 Entity Framework 5 而不是版本 6.

解决方案

首先,我们甚至不需要安装mysql-installer-社区-5.7.3.0-m13.msi.

  1. 安装最新的mysql-visualstudio-plugin
  2. 安装最新的mysql-connector-net
  3. 新的 C# .Net 4.5 框架 WinForms(对于 4.0,它应该基于 Entity Framework 6 是否支持 .NET 4.0? )
  4. 安装 4 个 Nuget Packages(按照顺序,如果你在 EntityFramework 之前安装 Mysql.Data.Entities,它将解决依赖并安装 EntityFramework 6.0.2 但我们需要的是是 EntityFramework 6.1.0)

实体框架

Mysql.Data

Mysql.Data.Entities

Mysql.Web

5.如果App.config中有标签entityFramework,请在标签启动后注释并在App.config中插入新标签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"/></提供者></entityFramework>

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 Files\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5(32 位窗口)

C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.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

When I tried to add ADO.NET Entity Data Model via

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

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.

  1. Install the latest mysql-visualstudio-plugin
  2. Install the latest mysql-connector-net
  3. New C# .Net 4.5 Framework WinForms (for 4.0 it should work based on Does Entity Framework 6 support .NET 4.0? )
  4. 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)

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

as your project reference for further EF6 functions.

这篇关于在 Microsoft Visual Studio 2013 的 WinForms 中为 MySql (C#) 启用实体框架 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:27
查看更多