c# - 使用 Mysql 设置 Entity Framework 6 - 代码优先

标签 c# mysql entity-framework

我正在尝试设置现有项目以使用 Entity Framework 。我以前从未使用过它,想在个人项目中学习它。

我有一个包含许多项目的解决方案,所有项目都相关。登录是我想做查询的地方。模型就是模型所在的地方。 Main 是程序启动的地方。

我已将 EntityFramework 安装到 MySolution.Model 上。

这是模型的 app.config:

<connectionStrings>
    <add name="ALDatabaseContext" providerName="MySql.Data.MySqlClient"
        connectionString="server=localhost;port=3306;database=aldatabase;uid=root;password=root"/>
</connectionStrings>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
    <provider invariantName="MySql.Data.MySqlClient"
      type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
     <provider invariantName="System.Data.SqlClient"
      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>

我的背景很简单

public class ALDatabaseContext : DbContext
{
    public virtual DbSet<User> Users { get; set; }
}

但是当我从登录调用上下文时出现异常:

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file.

我缺少什么?

最佳答案

好的,我设法让它工作(但我不喜欢这个解决方案)。

我已将其添加到 Main 的 app.config(我的解决方案的入口点)中:

<connectionStrings>
<add name="ALDatabaseContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=aldatabase;uid=root;password=root" />
</connectionStrings>



<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>


</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.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>

并且还在Login和Main项目中引用了Model的所有dll( Entity Framework 相关的dll和Mysql dll)。

关于c# - 使用 Mysql 设置 Entity Framework 6 - 代码优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43073834/

相关文章:

c# - 如何按顺序初始化 C# 中的字符串数组,其值从 "AAAAAA"到 "ZZZZZZ"

mysql - SQL - 如何用其他人替换具有 100 列的整个表中的字符?

c# - 如何在 asp.net mvc 中动态更改连接字符串

php - 将 mySQL 数据库中的数值分配给 PHP 变量时,最好使用什么方法?

c# - 为什么 EF 生成带有不必要的空值检查的 SQL 查询?

linq-to-sql - Entity Framework 4 的 SqlMetal 替代方案

c# - 为什么 EventInfo.RemoveEventHandler 抛出 NullReferenceException?

c# - 应该处置注入(inject)的属性吗?

C#/EmguCV - 将上传的 HttpPostedFileBase 转换为 Emgu.CV.Mat

mysql - 精确搜索和相似搜索结合两个表与多个关键字mysql