【发布时间】:2014-04-22 12:44:31
【问题描述】:
目前我正在编写一个 Windows 服务。
我已经在使用 EntityFramework 和 MSSQL 数据库,它运行良好。现在我必须并行使用 MySql。但我无法让它运行......我想避免使用 app.config 并通过从 DbContext 派生的类的构造函数配置 EntityFramework。
我有一个 SqlContext 类:
public class SqlContext : DbContext
{
public IDbSet<ServiceauftragSource> ServiceauftragSource { get; set; }
public SqlContext(string connectionString)
: base(connectionString)
{
}
public SqlContext(DbConnection connection)
: base(connection, true)
{
this.Configuration.LazyLoadingEnabled = true;
}
}
在我的 UnitOfWork 的构造函数中,我尝试创建我的 SqlContext:
public SqlUnitOfWork()
{
const string connStr = "server=127.0.0.1;uid=myuser;pwd=mypw;database=mydb;";
MySqlConnection conn = new MySqlConnection(connectionString);
this.Context = new SqlContext(conn);
}
这不起作用。尝试访问数据库时收到以下消息:
Unable to determine the DbProviderFactory type for connection of type 'MySql.Data.MySqlClient.MySqlConnection'. Make sure that the ADO.NET provider is installed or registered in the application config.
也没有:
public SqlUnitOfWork()
{
this.SetConnectionString();
this.Context = new SqlContext(connectionString);
}
private void SetConnectionString()
{
this.connectionString = "Data Source=" + debugDatabaseServer + ";Initial Catalog=" + debugDatabaseName
+ ";User ID=" + debugDatabaseUsername + ";Password=" + debugDatabasePassword
+ ";Trusted_Connection=False;Persist Security Info=True;";
}
我不知道为什么,但我认为这是因为我没有告诉我的上下文它的提供者(根据 SO 上的其他线程,它必须是 MySql.Data.MySqlClient)。但是在哪里以及如何?
项目参考:
更新
我尝试使用我的 App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="MySqlContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;
port=3306;database=***;uid=***;password=***"/>
</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>
</configuration>
访问数据库时出现以下错误:
Object reference not set to an instance of an object.
我认为这是因为我的服务在安装后无法访问 app.config。我确保 app.config 在构建后是 MyService.exe.config ...
【问题讨论】:
-
您在 app/web 配置中指定提供者
-
我试过了。请参阅更新的问题。仍然无法正常工作:(。
-
有任何解决方案吗?我遵循了mysql 的建议,但我总是收到错误消息“无法确定类型为'MySql.Data.MySqlClient.MySqlConnection' 的连接的 DbProviderFactory 类型。确保在应用程序中安装或注册了 ADO.NET 提供程序配置。”我的自定义 DbConfiguration 被正确调用,我在那里调用 SetDefaultConnectionFactory 和 SetProviderServices,但它没有帮助。
-
没有解决办法。我不再使用 MySQL 和 EF ...
标签: mysql visual-studio-2010 entity-framework windows-services entity-framework-6