【发布时间】:2016-07-21 00:24:03
【问题描述】:
在我的项目中,我尝试将实体框架与 PostgreSql 一起使用。但我无法连接到我的数据库。我没有收到任何错误,它只是卡住了。我认为我的app.config 有问题,但我无法找出原因。
App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />
<providers>
<provider invariantName="Npgsql"
type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql"
description="Data Provider for PostgreSQL"
type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="Entities"
connectionString="server=localhost;user id=postgres;password=4321;database=postgis"
providerName="Npgsql" />
</connectionStrings>
</configuration>
DbContext:
public class Entities : DbContext
{
public Entities() : base("Entities")
{
}
//rest of the code
}
mycode.cs
using (var db = new Entities()) // when debug it stuck here and keep running
{
// some test code
}
编辑:
我收到以下错误:
“无法加载在 ADO.NET 提供程序的应用程序配置文件中注册的实体框架提供程序类型 'Npgsql.NpgsqlServices, Npgsql.EntityFramework' 具有固定名称'Npgsql'。确保使用程序集限定名称并且该程序集可供正在运行的应用程序使用。
【问题讨论】:
-
错误很明显。提供者类型条目错误。 是否您的项目中有一个名为
Npgsql.EntityFramework.dll的程序集?您使用的是哪个软件包版本?在docs 中,程序集名称为Npgsql.EntityFrameworkLegacy.dll -
@PanagiotisKanavos 我有EntityFramework6.Npgsql 3.1.0.0版
-
@PanagiotisKanavos 你说得对,这就是问题所在,我没有注意到。如果您将评论作为答案,我可以接受。它可能会帮助一些人
标签: c# entity-framework postgresql npgsql