带有MySQL的实体框架6

时间:2015-02-25 19:59:03

标签: mysql asp.net entity-framework

我试图让Entity Framework 6与MySQL数据库一起工作。我正在遵循本指南:

http://bitoftech.net/2015/01/21/asp-net-identity-2-with-asp-net-web-api-2-accounts-management/

我用MySQL替换本地数据库。我安装了MySQL.NET Connector / Net Nuget包。但是,用我的主机替换连接字符串会给我带来麻烦。我无法使enable-migrations命令正常工作。在他们的FAQ页面上,我的主机声明您需要使用此连接字符串:

Server=myServerAddress;Database=MyDataBase;User=MyUser;Password=myPassword

这让我想到了这个连接字符串:

<add name="DefaultConnection" connectionString="Server=12.345.6.789:1234;Database=MyDatabaseName;User=MyUserName;Password=MySuperSecretPassword" providerName="System.Data.SqlClient" />

我设置了他们作为服务器给我的IP和端口号。请注意提供商名称。在控制台中运行enable-migrations时出现此错误:

  

访问数据库时出错。这通常意味着与数据库的连接失败。检查连接字符串是否正确,以及是否使用了相应的DbContext构造函数来指定它或在应用程序的配置文件中找到它。有关DbContext和连接的信息,请参阅http://go.microsoft.com/fwlink/?LinkId=386386。有关失败的详细信息,请参阅内部异常。

当我将providername切换为MySql.Data.MySqlClient时,出现此错误:

  

没有为ADO.NET提供程序找到具有不变名称&#39; MySql.Data.MySqlClient&#39;的实体框架提供程序。确保提供商已在&#39; entityFramework&#39;中注册。应用程序配置文件的一部分。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882

我做错了什么?使用EF6建立与MySQL数据库连接的最简单方法是什么?

编辑: 我目前的web.config:

<connectionStrings>
<add name="DefaultConnection" connectionString="Server=x.x.x.x;Database=RademaekAuthentication;User=x;Password=x" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

<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" />
</providers>

2 个答案:

答案 0 :(得分:1)

在您的web.config中,整个配置位于标记<configuration>下。你需要的任何地方

<entityFramework>
 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0" />
  </parameters>
 </defaultConnectionFactory>
 <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" />
 </providers>
</entityFramework>

答案 1 :(得分:0)

我最近遇到了这个问题并使用以下连接字符串解决了它:

<add name="DefaultMySqlConnection" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost; port=3306; Initial Catalog=<name>; uid=<user>; pwd=<password>;" />

Web.config还包含以下内容:

<entityFramework>
<defaultConnectionFactory
    type="System.Data.Entity.Infrastructure.SqlConnectionFactory,
          EntityFramework" />
<providers>
<provider
    invariantName="MySql.Data.MySqlClient"
    type="MySql.Data.MySqlClient.MySqlProviderServices,
          MySql.Data.Entity.EF6" />
</providers>

system.data标记中(如果它不存在,请在</configuration>标记之前创建),添加以下代码:

<DbProviderFactories>
  <remove invariant="MySql.Data.MySqlClient" /><add name="MySQL" description="ADO.Net driver for MySQL" invariant="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data"/>
</DbProviderFactories>

这是我的DbContext类:

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class DatabaseContext : DbContext
{
    public DatabaseContext(string connectionString)
        : base(connectionString)
    { }

在此之后确保运行迁移,希望它能够正常运行。

编辑:也许值得一提的是我的配置中没有<providers>部分。