gpt4 book ai didi

c# - Entity Framework + PostgreSQL 代码优先

转载 作者:行者123 更新时间:2023-11-29 12:28:16 27 4
gpt4 key购买 nike

第一次来。我正在尝试使用 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 数据包管理器的控制台中,发出 Enable Migrations 之后,我得到这个错误:

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, Func3 handleFailedLookup)<br/>
in System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key)<br/>
in System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple
2 k)
in System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 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.WhereSelectArrayIterator2.MoveNext()<br/>
in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
1 source, Func2 predicate)<br/>
in System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)<br/>
in System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(Type type, Object key)<br/>
in System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r)<br/>
in System.Linq.Enumerable.WhereSelectArrayIterator
2.MoveNext()
in System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate)
in System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
in System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver2.GetService(Type type, Object key)<br/>
in System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key)<br/>
in System.Data.Entity.Internal.LazyInternalConnection.CreateConnectionFromProviderName(String providerInvariantName)<br/>
in System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)<br/>
in System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)<br/>
in System.Data.Entity.Internal.LazyInternalConnection.Initialize()<br/>
in System.Data.Entity.Internal.LazyInternalConnection.get_Connection()<br/>
in System.Data.Entity.Internal.LazyInternalContext.get_Connection()<br/>
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.

你能帮帮我吗?

提前致谢。

最佳答案

尝试使用 Windows Installer 将 Npgsql 安装到 GAC。这在很多时候都有帮助。

Download

此外,使用以下方法检查 DbProviderFactories 中的版本号:

typeof(Npgsql.NpgsqlFactory).AssemblyQualifiedName

并确保它们匹配。

关于c# - Entity Framework + PostgreSQL 代码优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43261287/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com