实体框架+ PostgreSQL代码优先

问题描述 投票:2回答:2

第一次来这里。我正在尝试使用Visual Studio 2017社区将Entity Framework与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 Package Manager控制台中,在发出qazxsw poi后,我收到此错误:

检查上下文是否以现有数据库为目标... 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中(类型类型,对象键,FuncEnable Migrations2 k) 在System.Collections.Concurrent.ConcurrentDictionary3 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 valueFactory) 在System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(Type type,Object key)中 在System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain。<> c__DisplayClass3.b__0(IDbDependencyResolver r) 在System.Linq.Enumerable.WhereSelectArrayIterator2.GetOrAdd(TKey key, Func1源代码中,Func2.MoveNext()
in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
2.MoveNext() 在System.Linq.Enumerable.FirstOrDefault [TSource](IEnumerable2 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谓词)中 在System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type,Object key)中 在System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver1 source, Func1解析器中) 在System.Data.Entity.Migrations.DbMigrator..ctor中(DbMigrationsConfiguration配置,DbContext usersContext,DatabaseExistenceState existState,Boolean calledByCreateDatabase) 在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(String language,String rootNamespace)中 在System.Data.Entity.Migrations.EnableMigrationsCommand。<> c__DisplayClass2。<。ctor> b__0() 在System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action命令)中

无法找到或加载已注册的.Net Framework数据提供程序。

你能帮助我吗?

提前致谢。

c# .net postgresql entity-framework
2个回答
0
投票

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

2.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

另外,使用以下命令检查Download中的版本号:

DbProviderFactories

并确保它们匹配。


-2
投票

我前段时间使用过本教程typeof(Npgsql.NpgsqlFactory).AssemblyQualifiedName

© www.soinside.com 2019 - 2024. All rights reserved.