我正在尝试使用Npgsql 2.0.14.3测试Entity Framework 6迁移,以完成我的开源PostgreSqlMigrationSqlGenerator库的支持,该库允许将EF迁移与Postgresql一起使用。
我写的测试类是这个(click here for github page):
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations;
using System.Data.Entity;
using System.Data.Entity.Migrations.Sql;
using Npgsql;
using NUnit.Framework;
namespace EntityFramework.PostgreSql.Test.IntegrationTests
{
[TestFixture]
public class PostgreSqlMigrationSqlGeneretorHistoryTest
{
private const string ConnectionString = "Server=127.0.0.1;Port=5432;Database=testEF6;User Id=postgres;Password=p0o9i8u7y6;CommandTimeout=20;Preload Reader = true;";
private const string ProviderName = "Npgsql";
[Test]
public void GenerateInsertHistoryOperation()
{
var migrator = new DbMigrator(new LocalMigrationConfiguration());
migrator.Update();
}
public class LocalMigrationConfiguration : DbMigrationsConfiguration<LocalPgContext>
{
public LocalMigrationConfiguration()
{
AutomaticMigrationDataLossAllowed = true;
AutomaticMigrationsEnabled = false;
SetSqlGenerator("Npgsql", new PostgreSqlMigrationSqlGenerator());
MigrationsNamespace = "EntityFramework.PostgreSql.Test.IntegrationTests.Migrations";
MigrationsAssembly = typeof (LocalPgContext).Assembly;
TargetDatabase = new DbConnectionInfo(ConnectionString, ProviderName);
}
}
public class LocalPgContext : DbContext//, IDbProviderFactoryResolver, IDbConnectionFactory
{/*
public DbProviderFactory ResolveProviderFactory(DbConnection connection)
{
return DbProviderFactories.GetFactory("Npgsql");
}
public DbConnection CreateConnection(string nameOrConnectionString)
{
return new NpgsqlConnection(nameOrConnectionString);
}*/
}
/*
public class LocalConfiguration : DbConfiguration
{
public LocalConfiguration()
{
// can't set this cos NpgsqlServices is internal
SetProviderServices(
"Npgsql", provider: NpgsqlServices.Instance
);
}
}
*/
}
}
测试方法GenerateInsertHistoryOperation
未初始化,因为它返回此错误:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information:
The 'Instance' member of the Entity Framework provider type 'Npgsql.NpgsqlServices, Npgsql, Version=2.0.14.3, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' did not return an object that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'.
Entity Framework providers must inherit from this class and the 'Instance' member must return the singleton instance of the provider.
This may be because the provider does not support Entity Framework 6 or later; see http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
我添加了一个App.confing文件来设置提供程序(github link):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework" requirePermission="false" />
</configSections>
<entityFramework>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql" />
</providers>
</entityFramework>
</configuration>
此时,我不知道Npgsql 2.0.14.3是否仍然不支持EF6,或者我是否遗漏了我的代码。
Click here to see it on github
感谢。
答案 0 :(得分:2)
我很想看到完成的项目,因为PostgreSQL迁移在我最近的项目中非常有用,不幸的是使用EF 6。
我认为你需要的npgsql版本是最新的beta版本,试着在包管理器中使用这一行来安装npgsql的EF 6版本
Install-Package Npgsql.EF6 -Pre