Postgresql 和实体框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36352203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:13:03  来源:igfitidea点击:

Postgresql and Entity Framework

c#entity-frameworkpostgresqlnpgsql

提问by N.J

In my project I am trying to use Entity Framework along with PostgreSql. But I am not able to connect to my database. I am not getting any error, it just gets stuck. I think something is wrong with my app.config, but I am not able to find out what.

在我的项目中,我试图将实体框架与 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:

DbContext

public class Entities : DbContext
{
    public Entities() : base("Entities")
    {
    }

    //rest of the code
}

mycode.cs

我的代码.cs

using (var db = new Entities()) // when debug it stuck here and keep running 
{
 // some test code
}

EDIT:

编辑:

I get the following error :
"The Entity Framework provider type 'Npgsql.NpgsqlServices, Npgsql.EntityFramework' registered in the application config file for the ADO.NET provider with invariant name 'Npgsql' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application.

我收到以下错误:
“无法加载在具有不变名称“Npgsql”的 ADO.NET 提供程序的应用程序配置文件中注册的实体框架提供程序类型“Npgsql.NpgsqlServices、Npgsql.EntityFramework”。请确保程序集-使用限定名称并且程序集可用于正在运行的应用程序。

回答by Panagiotis Kanavos

The problem points to a wrong provider type or assembly name.

问题指向错误的提供程序类型或程序集名称。

<entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />
    <providers>
        <provider invariantName="Npgsql" 
                  type="Npgsql.NpgsqlServices, Npgsql.EntityFramework"  />
    </providers>
</entityFramework>

The assembly name is wrong. The assembly installed by the EntityFramework6.Npgsql package is EntityFramework6.Npgsql.dll. In fact, adding the package to a new project sets the correct provider line:

程序集名称错误。EntityFramework6.Npgsql 包安装的程序集是EntityFramework6.Npgsql.dll. 实际上,将包添加到新项目会设置正确的提供程序行:

<providers>
  <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>