【问题标题】:Entity Framework + PostgreSQL code-first实体框架 + PostgreSQL 代码优先
【发布时间】:2017-09-01 20:16:12
【问题描述】:

第一次来。我正在尝试使用 Visual Studio 2017 社区将实体框架与 PostgreSQL 集成:

从 Nuget,我安装了这个包:

  • EntityFramework.6.1.3
  • Npgsql.3.2.2
  • EntityFramework6.Npgsql.3.1.1

我的项目是一个带有 .NET Framework 4.6.2(非核心)的 Web API

我的web.config 看起来像这样

<system.data>
    <DbProviderFactories>
        <remove invariant="Npgsql"/>
        <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=4.0.2.0, Culture=neutral, PublicKeyToken=5D8B90D52F46FDA7"/>
    </DbProviderFactories>
</system.data>
<entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql"/>
    <providers>
        <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql"/>
    </providers>
</entityFramework>

但是,在 Nuget Packet Manager 的控制台中,发出 Enable Migrations 后,我收到此错误:

检查上下文是否针对现有数据库...
System.Configuration.ConfigurationErrorsException: 未能找到或加载已注册的 .Net Framework 数据提供程序。

在 System.Data.Common.DbProviderFactories.GetFactory(DataRow providerRow)
在 System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
在 System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(类型类型,对象键,Func3 handleFailedLookup)
in System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key)
in System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple
2 k)
在 System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
在 System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(类型类型,对象键)
在 System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.c__DisplayClass3.b__0(IDbDependencyResolver r)
在 System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext()
in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
1 源中,Func2 predicate)
in System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
in System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(Type type, Object key)
in System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r)
in System.Linq.Enumerable.WhereSelectArrayIterator
2.MoveNext()
在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 谓词)
在 System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(类型类型,对象键)
在 System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver2.GetService(Type type, Object key)
in System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key)
in System.Data.Entity.Internal.LazyInternalConnection.CreateConnectionFromProviderName(String providerInvariantName)
in System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)
in System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)
in System.Data.Entity.Internal.LazyInternalConnection.Initialize()
in System.Data.Entity.Internal.LazyInternalConnection.get_Connection()
in System.Data.Entity.Internal.LazyInternalContext.get_Connection()
in System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func
1 解析器)
在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration 配置,DbContext usersContext,DatabaseExistenceState 存在状态,布尔调用ByCreateDatabase)
在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration 配置)
在 System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
在 System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
在 System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
在 System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(字符串语言,字符串根名称空间)
在 System.Data.Entity.Migrations.EnableMigrationsCommand.c__DisplayClass2.<.ctor>b__0()
在 System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action 命令)

未能找到或加载已注册的 .Net Framework Data Provider。

你能帮帮我吗?

提前致谢。

【问题讨论】:

    标签: c# .net postgresql entity-framework


    【解决方案1】:

    尝试使用 Windows Installer 将 Npgsql 安装到 GAC。这可以在很多时候提供帮助。

    Download

    另外,检查DbProviderFactories 中的版本号:

    typeof(Npgsql.NpgsqlFactory).AssemblyQualifiedName
    

    并确保它们匹配。

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 请在此处给出答案,因为 URL 将来可能会损坏。
      • ;) 链接已损坏。
      猜你喜欢
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 2012-05-31
      • 2012-12-30
      • 2015-04-09
      相关资源
      最近更新 更多