昨天我知道 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包
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)
当我尝试通过
添加 ADO.NET 实体数据模型时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
然后生成实体连接字符串->勾选将App.Config中的实体连接设置保存为->下一步>->
您要使用哪个版本的 Entity Framework ? 有选项 Entity Framework 6.0 但你不能使用它,因为
"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".
如何解决?
顺便说一句,如果您在 Nuget 包中安装 Entity Framework 版本 5,那么您可能会在此处选择 Entity Framework 5.0,您可能会成功使用 Entity Framework 5 而不是版本 6。
最佳答案
首先,我们甚至不需要安装 mysql-installer-community-5.7.3.0-m13.msi。
EntityFramework
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" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
6.添加ADO.NET实体数据模型(如问题所述)
7.After Entity Connection string is generated(如问题所述)并在App.Config中勾选Save entity connection settings as然后单击Next
8.选择您的数据库对象和设置(表、 View 或存储过程和函数)(没有“您要使用哪个版本的 Entity Framework ?”因为我有只有一个 Entity Framework 6.0 提供者,如果我唯一的提供者有效,请直接跳过选择)
9.完成
恭喜^^
顺便说一句,您可能需要添加 .dll 文件
在这个文件夹中
C:\Program Files\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5 (32bit windows)
C:\Program Files (x86)\MySQL\MySQL Connector Net 6.8.3\Assemblies\v4.5 (64bit windows)
作为您的项目引用,以获取更多 EF6 功能。
关于c# - 在 Microsoft Visual Studio 2013 的 WinForms 中启用 Entity Framework 6 for MySql (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22031269/