我正在尝试使用 Npgsql 2.0.14.3 测试 Entity Framework 6 迁移,以完成对我的开源 PostgreSqlMigrationSqlGenerator 库的支持,该库允许将 EF 迁移与 Postgresql 一起使用。

我正在编写的测试类是这样的(单击此处查看 github 页面):

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链接):

    <?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 或者我的代码中是否缺少某些内容。

点击这里在 github 上查看

谢谢。

有帮助吗?

解决方案

我很高兴看到已完成的项目,因为 PostgreSQL 迁移在我最新的项目中非常有用,不幸的是,该项目使用的是 EF 6。

我认为您需要的 npgsql 版本是最新的 beta 版本,尝试使用包管理器中的这一行来安装 EF 6 版本的 npgsql

Install-Package Npgsql.EF6 -Pre
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top