简体   繁体   中英

Entity Framework + PostgreSQL code-first

First time here. I'm trying to integrate Entity Framework with PostgreSQL using Visual Studio 2017 Community:

From Nuget, I installed this packages:

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

My project is a Web API with .NET Framework 4.6.2 (not core)

My web.config looks like this

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

But, in the Nuget Packet Manager's console, after issuing Enable Migrations , I get this error:

Checking if the context targets an existing database...
System.Configuration.ConfigurationErrorsException: Failed to find or load the registered .Net Framework Data Provider.

in System.Data.Common.DbProviderFactories.GetFactory(DataRow providerRow)
in System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
in System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key, Func 3 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
3 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
3 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)

in System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func 2 valueFactory)
in System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(Type type, Object key)
in System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.b__0(IDbDependencyResolver r)
in System.Linq.Enumerable.WhereSelectArrayIterator 2.MoveNext()
in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
2.MoveNext()
in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
2.MoveNext()
in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
1 source, Func 2 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 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 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()

in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable 1 source, Func 2 predicate)
in System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
in System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver 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
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
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
1 resolver)

in System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
in System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
in System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
in System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
in System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
in System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
in System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
in System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
in System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
in System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

Failed to find or load the registered .Net Framework Data Provider.

Can you help me?

Thanks in advance.

Try installing Npgsql to the GAC with the Windows Installer. This can help a lot of the time.

Download

Also, check the version number in the DbProviderFactories using:

typeof(Npgsql.NpgsqlFactory).AssemblyQualifiedName

And make sure they match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM