连接字符串首先在 PostgreSQL 代码中意外更改

Connection string changed unexpectedly in PostgreSQL code first

1- 已安装 Npgsql 3.1.9.0EntityFramework6.Npgsql.dll 31.0.0 dll

2- 定义一个DbContext如下

  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();

异常:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

但我可以看到连接是默认的,是连接字符串不正确还是其他原因?

更新:

经过多次尝试和一次又一次安装 Npgsql 我知道 <configSections> 必须是根 <configuration> 的第一个 child,更正它并 运行 再次希望 运行 正确,但最后我得到了这个例外:

Could not load file or assembly 'Npgsql, Version=3.1.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

我认为需要参考一些依赖关系,但我是 Npgsl 的新手,谁能知道哪里出了问题。

是的,一分耕耘一分收获,对开发者来说没有任何障碍!

Npgsql 遇到许多问题并首先编写代码后,我成功地 运行 我截取的代码,但这里有一些注释对某些人有用

正如他们所说,通过 Nuget 安装 Install-Package EntityFramework6.NpgsqlNpgsql 及其所有依赖项将被毫无问题地引用,但我遇到了很多失败,因此通过安装 Install-Package EntityFramework6.Npgsql 以下库被引用为下面给出的 desc(2016 年 11 月 22 日,星期二):

1- Npgsql 版本 3.1.0.0

2-EntityFramework6.Npgsql 版本 3.1.1.0

3-EntityFrameworkEntityFramework.SqlSrver 版本 6.0.0.0

建立 DbContext 和数据模型后,您可能会遇到异常

Could not load file or assembly 'Npgsql, Version=3.1.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

因为Ngpsql不兼容Entityframework6.Npgsql或者其他原因应该位于Ngpsql位于C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Npgsql\v4.0_3.1.9.0__5d8b90d52f46fda7是3.1.9

但为了绕过这个异常,我手动添加了 Npgsql 版本 3.1.9.0,然后像 运行 一样编写代码。

所以 App.config 应该是:

    <?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>

希望这对你有用。