2017-02-18 168 views
0

我一直在寻找相当一段时间寻找这方面的一些方向..无法找到答案。Postgres&C#:无法加载类型Npgsql.NpgsqlConnectionFactory

尝试通过使用.net实体框架连接到PostgreSQL(似乎是合理的,而不是围绕普通的sql连接方法编写我自己的框架)。

在exeecuting的以下几行代码,我得到如下的错误如下:

`using (var db = new dbTestContext()) 
{ 
var test = from a in db.dbTest select a; 
dataGridView1.DataSource = test.ToList();    
}` 

我的错误: 外层的异常:

{"Failed to set Database.DefaultConnectionFactory to an instance of the 'Npgsql.NpgsqlConnectionFactory, Npgsql' type as specified in the application configuration. See inner exception for details."}

内部异常:

{"Could not load type 'Npgsql.NpgsqlConnectionFactory' from assembly 'Npgsql'.":"Npgsql.NpgsqlConnectionFactory"}

设置: 我已经从NugetPacka安装了npgsql ge经理 我已经从Nuget包管理器安装Entitiy Framework 6 for npgsql

我有以下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> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
    </startup> 
    <entityFramework> 
    <!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />-->  
    <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql" /> 
    <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.2.1.0" newVersion="3.2.1.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <system.data> 
    <DbProviderFactories>  
     <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" /> 
    </DbProviderFactories> 
    </system.data> 
    <connectionStrings> 
    <add name="dbTestConnectionString" connectionString="Server=localhost;Database=dbTest;User Id=postgres;Password=Dve835167!;" providerName="Npgsql" /> 
    </connectionStrings> 
</configuration>` 

任何方向,将不胜感激

+0

删除以前的文本 –

回答

1

看来我找到了解决办法。 dbContext没有通过连接字符串链接到数据库实例。下面的解决方案可以在调用基类构造函数的地方看到。

class dbTestContext: DbContext 
    { 

     //=below= connection string now has connection string identifier as argument 
     public dbTestContext() : base("dbTestConnectionString") { } 

     public DbSet<Test> dbTest { get; set; }  

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 

      modelBuilder.Entity<Test>().ToTable("tblTest", "public");    


      modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>(); 
     } 
    } 
} 

如果我可以发表评论:使用EF和Link2sql语法简单查询(同原来的问题)的第一次执行似乎很慢 - 大约3倍,通过sqlconnector和命令直接SQL一样慢。然而,第二次运行它是立即的(仅在数据库中有7行)。将做一些进一步的研究。

+0

作为旁注(如果允许),EF的速度问题似乎与dbContext的实例化有关。使用两个单独的查询进行测试,只有同一个dbContext中的第一次执行显示开销。使用模块化的dbContexts(如果您拥有大量的实体/表),似乎也很谨慎。 –

相关问题