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

时间:2016-11-21 16:21:32

标签: postgresql ef-code-first npgsql

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

例外:

  
    

建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)

  

但我可以看到连接是默认的,是连接字符串不正确还是其他什么? enter image description here

更新

经过多次尝试并一次又一次地安装Npgsql我知道<configSections>必须是根<configuration>的第一个孩子,纠正它并再次运行希望运行正常但是在结束我有这个例外:

  
    

无法加载文件或程序集&#39; Npgsql,Version = 3.1.2.0,Culture = neutral,PublicKeyToken = 5d8b90d52f46fda7&#39;或其中一个依赖项。定位的程序集的清单定义与程序集引用不匹配。 (HRESULT异常:0x80131040)

  

我认为有些dependensies需要被引用,但我是Npgsl的新手,任何人都知道出了什么问题。

1 个答案:

答案 0 :(得分:0)

是,没有痛苦没有收获,开发者没有任何障碍!

Npgsql和代码问题出现很多问题之后,我成功运行了我的剪切代码,但这里有一些注释我对某人有用

正如他们通过Install-Package EntityFramework6.Npgsql Nuget安装Npgsql所说的那样,所有它的依赖项都会被引用而没有任何问题,但是我已经有很多失败因此安装了Install-Package {{1下面的库被引用为deslow(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应该:

App.config

希望这对你有用。