首先使用 ef6 连接到 godaddy 上的 MySql 数据库实现代码时遇到问题。它仍在尝试连接到 SQL 实例
运行 update-database
时出现以下错误
Error Number:53,State:0,Class:20 A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
这是我的应用程序配置:
<xml version="1.0" encoding="utf-8">
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<connectionStrings>
<add name="LMScontext" connectionString="Server=servername;Database=dbname;Uid=user;Pwd=password;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<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" />-->
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</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.10.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
</configuration>
最佳答案
适合我的情况的一个可能的解决方案是,
在 <providers>.....</providers>
上方的行下方添加
<defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
<providers>
...
</providers>
然后删除<DbProviderFactories>.....</DbProviderFactories>
部分来自web.config
或app.config
.
然后将以下属性添加到 DbContext
类实现
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
所以它看起来像,
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class MySqlDbContext : DbContext
{
.....
}
关于c# - 为什么我无法使用 ef6 code First mysql 迁移来更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55194934/