1-已安装Npgsql 3.1.9.0EntityFramework6.Npgsql.dll 31.0.0dll
2-定义aDbContext如下

  public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("myConnectionString")
    {

    }
    public virtual DbSet<Tag> Tags { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("public");
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

3-和我的app.config
<configuration>
  <connectionStrings>
    <add name="myConnectionString" providerName="Npgsql" connectionString="Host=localhost;Port=5432;Database=mv_test;User Id=postgres;Password=devel;" />
  </connectionStrings>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
    </providers>
  </entityFramework>
</configuration>

4-我的测试截短代码
 MyDbContext myContext = new MyDbContext();
 int c = myContext.Tags.Count();

例外情况:
建立与SQL Server的连接时发生与网络相关或特定于实例的错误。找不到或无法访问服务器。请验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:SQL网络接口,错误:26-定位指定的服务器/实例时出错)
但我可以看到连接是默认的,是连接字符串不正确还是其他原因?
postgresql - 首先在PostgreSQL代码中意外更改了连接字符串-LMLPHP
更新:
经过多次尝试并一次又一次地安装Npgsql之后,我知道<configSections>必须是根<configuration>的第一个子级,因此对其进行了更正并再次运行希望能够正常运行,但最后出现了以下异常:
无法加载文件或程序集“Npgsql,Version=3.1.2.0,Culture=neutral,PublicKeyToken=5d8b90d52f46fda7”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT的异常:0x80131040)
我认为有些家属需要参考,但我是新来的Npgsl谁都知道哪里出了问题。

最佳答案

是的,没有痛苦就没有收获,对于一个开发者来说没有任何障碍!
在使用Npgsql和代码时遇到许多问题之后,我首先成功地运行了被截取的代码,但这里有一些注释,我对某些人很有用
正如他们所说,通过安装Install-Package EntityFramework6.NpgsqlNuget和它的所有依赖项都将被引用,没有任何问题,但我有许多失败,因此通过安装Install PackageNpgsql,以下库将被引用为下面给出的描述(2016年11月22日,星期二):
1-EntityFramework6.Npgsql版本3.1.0.0
2-Npgsql版本3.1.1.0
3-EntityFramework6.NpgsqlEntityFramework版本6.0.0.0
在建立EntityFramework.SqlSrver和数据模型之后,您可能会得到
无法加载文件或程序集“Npgsql,Version=3.1.2.0,Culture=neutral,PublicKeyToken=5d8b90d52f46fda7”或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT的异常:0x80131040)
因为DbContextNgpsql不兼容,或者另一个原因应该是Entityframework6.Npgsql位于Ngpsql中是3.1.9
但是为了绕过这个异常,我手动添加了C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Npgsql\v4.0_3.1.9.0__5d8b90d52f46fda7版本3.1.9.0,然后代码像一个符咒一样运行。
所以Npgsql应该是:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
   <connectionStrings>
    <add name="myConnectionString" providerName="Npgsql" connectionString="Host=localhost;Port=5432;Database=mv_test;User Id=postgres;Password=devel;" />
  </connectionStrings>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

希望这对你有用。

关于postgresql - 首先在PostgreSQL代码中意外更改了连接字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40724719/

10-12 12:43
查看更多