简体   繁体   English

Postgresql和实体框架

[英]Postgresql and Entity Framework

In my project I am trying to use Entity Framework along with PostgreSql. 在我的项目中,我试图使用Entity Framework和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. 我觉得我的app.config ,但我无法找到答案。

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 mycode.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'。确保使用了程序集限定名称并且程序集可供正在运行的应用程序使用。

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 . EntityFramework6.Npgsql包安装的程序集是EntityFramework6.Npgsql.dll In fact, adding the package to a new project sets the correct provider line: 实际上,将包添加到新项目会设置正确的提供者行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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