实体框架5.0的PostgreSQL(Npgsql)默认连接工厂 [英] Entity Framework 5.0 PostgreSQL (Npgsql) default connection factory
问题描述
我试图让EF 5.0 code第一次使用的PostgreSQL(Npgsql提供商)。我有Npgsql 2.0.12.1通过的NuGet安装(引用的程序集是2.0.12.0虽然)。 我有Npgsql中的app.config声明(包括默认连接工厂和供应商的工厂):
I'm trying to get EF 5.0 code first working with PostgreSQL (Npgsql provider). I have Npgsql 2.0.12.1 installed via NuGet (referenced assembly is 2.0.12.0 though). I have Npgsql declared in app.config (both default connection factory and provider factory):
<entityFramework>
<defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider"
invariant="Npgsql"
description="Data Provider for PostgreSQL"
support="FF"
type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"/>
</DbProviderFactories>
</system.data>
我有以下的测试运行成功:
I have following test running successfully :
[Test]
public void DatabaseConnection_DatabaseFactoryTest()
{
var factory = DbProviderFactories.GetFactory("Npgsql");
var conn = factory.CreateConnection();
conn.ConnectionString = _connectionString;
var npg = (NpgsqlConnection)conn;
var result = TestConnectionHelper(npg); // scalar select version(), nothing particular
Assert.AreEqual(result, "PostgreSQL 9.2.2, compiled by Visual C++ build 1600, 64-bit");
}
这意味着至少有数据库实例运行,并提供配置成功。 现在,我想是使用自定义的数据库上下文从的DbContext继承,将通过连接字符串来连接到相同的提供者和初始化的:
That means at least database instance is running and provider is configured successfully. Now what i want is to use custom database context inherited from DbContext which will be tied to same provider and initialized via connection string :
public class InventoryContext : DbContext
{
public InventoryContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
// mappings and properties, cut for conciseness
}
下面的测试失败:
Following test fails :
[Test]
public void DatabaseConnection_DatabaseContextTest()
{
using (var ctx = new InventoryContext(_connectionString))
{
//var db = ctx.Database;
ctx.InventoryObjects.Add(_inventoryObject); // exception here
ctx.SaveChanges();
}
}
报告说,
Failed to set Database.DefaultConnectionFactory to an instance of the 'Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' type as specified in the application configuration. See inner exception for details.
内部异常是出现InvalidOperationException:
Inner exception is InvalidOperationException :
{"Constructor for type \"Npgsql.NpgsqlFactory\" is not found."}
我想有一个与连接字符串的问题(不包含Npgsql提供商):
I guess there is a problem with connection string (it does not contain Npgsql provider) :
"Server=127.0.0.1;Port=5432;User Id=postgres;Password=p4ssw0rd;Database=InventoryDatabase;";
什么是最优雅的方式以编程方式解决这个问题?刚试过路过的connectionString从app.config中上下文的构造,它的工作原理。
修改
上传的测试项目,Dropbox的 - VS2012的解决方案,10 MB
What is the most elegant way to solve this problem programmatically? Just tried passing connectionString from app.config to context's constructor, it works.
edit
Uploaded test project to Dropbox - VS2012 solution, 10 mb
推荐答案
更深入地挖掘问题,发现它是由这一事实,Npgsql所引用的EntityFramework 4.4.0组装。解决如下:
Dug deeper into the problem, found out that it is caused by that fact that Npgsql is referencing EntityFramework 4.4.0 assembly. Solved as follows :
- 新增EF的NuGet包测试项目(这是建立对FW 4.5);
- 在Npgsql2010项目中手动添加引用EntityFramework.dll 5版本(的NuGet增加了4.4.0默认情况下);
- 改变组件Npgsql的app.config绑定的的的EntityFramework,版本= 5.0.0.0,文化=中性公钥= b77a5c561934e089的;
- 修正通过构造公共上面的的构造类型Npgsql.NpgsqlFactory找不到的错误描述;
-
修正了以下错误的的无法投NpgsqlFactory到IDbConnectionFactory的通过实现IDbConnectionFactory接口NpgsqlFactory:
- Added EF Nuget package to test project (which is build against FW 4.5);
- Manually added reference to EntityFramework.dll version 5 in Npgsql2010 project (Nuget adds 4.4.0 by default);
- Changed assembly binding in Npgsql app.config to "EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
- Fixed described above "Constructor for type "Npgsql.NpgsqlFactory" is not found error by making constructor public;
Fixed following error "Unable cast NpgsqlFactory to IDbConnectionFactory" by implementing IDbConnectionFactory interface on NpgsqlFactory :
使用System.Data.Entity.Infrastructure;
...
公共密封类NpgsqlFactory:DbProviderFactory,IServiceProvider的,IDbConnectionFactory
...
公众的DbConnection创建连接(字符串nameOrConnectionString)
{
返回新NpgsqlConnection(nameOrConnectionString);
}
ð
using System.Data.Entity.Infrastructure;
...
public sealed class NpgsqlFactory : DbProviderFactory, IServiceProvider, IDbConnectionFactory
...
public DbConnection CreateConnection(string nameOrConnectionString)
{
return new NpgsqlConnection(nameOrConnectionString);
}
d
现在我遇到错误:3F000:模式DBO不存在,这是关系到EF。 (表名,大众),虽然modelBuilder.Entity()ToTable:我已经映射到PostgreSQL标准的公共架构中的DbContext的OnModelCreating。期待这个问题的解决方案。
Now i'm experiencing "Error: 3F000: schema "dbo" doesn't exist" which is related to EF. I have mapping to PostgreSQL standard public schema in DbContext's OnModelCreating : modelBuilder.Entity().ToTable("TableName", "public") though. Looking forward to solution of this problem.
这篇关于实体框架5.0的PostgreSQL(Npgsql)默认连接工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!