为什么我不能先使用 ef6 代码 mysql 迁移来更新数据库?
Why can't I update database using ef6 code first mysql migrations?
首先使用 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)
这是我的 app.config:
<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>
尝试this。我认为这可能会有帮助
一种适用于我的情况的可能解决方案是,
在 <providers>.....</providers>
正上方的行下方添加
<defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
<providers>
...
</providers>
然后从 web.config
或 app.config
中删除 <DbProviderFactories>.....</DbProviderFactories>
部分。
然后将以下属性添加到 DbContext
class 实现
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
所以它看起来像,
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class MySqlDbContext : DbContext
{
.....
}
首先使用 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)
这是我的 app.config:
<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>
尝试this。我认为这可能会有帮助
一种适用于我的情况的可能解决方案是,
在 <providers>.....</providers>
<defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
<providers>
...
</providers>
然后从 web.config
或 app.config
中删除 <DbProviderFactories>.....</DbProviderFactories>
部分。
然后将以下属性添加到 DbContext
class 实现
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
所以它看起来像,
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class MySqlDbContext : DbContext
{
.....
}