Reputation: 105
I have been searching quite some time for some direction on this..unable to find the answer.
Trying to connect to PostgreSQL by using .net Entity Framework (seems sensible, instead of writing my own framework around the normal sql connection method).
On exeecuting the following lines of code, I get the error that follows below:
`using (var db = new dbTestContext())
{
var test = from a in db.dbTest select a;
dataGridView1.DataSource = test.ToList();
}`
My Error: Outer Exception:
{"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."}
Inner Exception:
{"Could not load type 'Npgsql.NpgsqlConnectionFactory' from assembly 'Npgsql'.":"Npgsql.NpgsqlConnectionFactory"}
Setup: I have installed npgsql from NugetPackage Manager I have installed Entitiy Framework 6 for npgsql from Nuget Package Manager
I have the following App.Config
file (apologies, my first post, still trying to get the formatting correct).
`<?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>`
Any direction would be appreciated
Upvotes: 0
Views: 8307
Reputation: 105
It seems I found a solution. the dbContext was not linked to the database instance via a connection string. Solution can be seen below where the base class constructor is called.
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>();
}
}
}
If I may comment: The first execution of a simple query (same as in original question) using EF and Link2sql syntax seems very slow - roughly 3 times as slow as direct SQL via sqlconnector and command. However running it for a second time, is immediate (only having 7 rows in the database). Will do some further research one this.
Upvotes: 1