gpt4 book ai didi

mysql - 带有 MySQL 的 Entity Framework 6

转载 作者:行者123 更新时间:2023-11-29 02:57:40 24 4
gpt4 key购买 nike

我正在尝试让 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 命令正常工作。在他们的常见问题解答页面上,我的主机声明您需要使用此连接字符串:

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 时出现此错误:

An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.

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

No Entity Framework provider found for the ADO.NET provider with invariant name 'MySql.Data.MySqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

我做错了什么?使用 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>

最佳答案

在您的 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>

关于mysql - 带有 MySQL 的 Entity Framework 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28728459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com