如何通过连接使用Entity Framework + PostgreSQL? [英] How to use Entity Framework + PostgreSQL from connection?
问题描述
我已经看到一些线程在官方指导下讨论Entity Framework和PostgreSQL的使用。这些说明需要在每次安装时运行 gacutil
,这对于部署目的不是很方便。
I have already seen threads discussing the use of Entity Framework and PostgreSQL with official instructions. Those instructions need to run gacutil
for every install which is not so handy for deployment purposes.
我要做什么这样做是将PostgreSQL连接直接传递到 DbContext
构造函数。这对我来说已经足够了,因为我将在没有设计器的情况下使用 CodeFirst
。这就是我的工作:
What I want to do here is passing PostgreSQL connection directly to the DbContext
constructor. This is enough for me because I am going to use CodeFirst
without designer. This is what I do:
public class Context : DbContext
{
Context(System.Data.Common.DbConnection connection)
: base(connection, true)
{
}
public static Context CreateContext()
{
NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=postgres;Password=********;Database=xxx;");
conn.Open();
return new Context(conn);
}
}
但是使用这种方法,我得到了 NotSupportedException
并显示消息:
But using this method I get a NotSupportedException
with message:
无法确定类型为
的连接的提供程序名称Npgsql.NpgsqlConnection'。
Unable to determine the provider name for connection of type 'Npgsql.NpgsqlConnection'.
我该怎么办?
推荐答案
您需要在 app / web.config
中注册Npgsql提供程序。请参阅Npgsql手册的 3.4将Npgsql与ProviderFactory一起使用。
You'll need to register the Npgsql provider in the app/web.config
. See section 3.4 Using Npgsql with ProviderFactory of the Npgsql manual.
当您为数据库(MySQL,PostgreSQL等)安装ADO.NET提供程序时,安装程序通常会在GAC中注册该提供程序程序集并将条目添加到 machine.config
。如果要部署而不必安装提供程序,则需要包括提供程序程序集的副本(将Npgsql程序集引用设置为项目的本地复制),并将条目添加到应用程序的 app / web.config
如下:
When you install an ADO.NET provider for databases (MySQL, PostgreSQL, etc.) the installers will usually register the provider assembly in the GAC and add an entry to the machine.config
. If you want to deploy without having to install the provider you'll need to include a copy of the provider assembly (set Npgsql assembly reference as Copy Local for your project) and add an entry to your application's app/web.config
as follows:
<configuration>
...
<system.data>
<DbProviderFactories>
<clear />
<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql Server" type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</DbProviderFactories>
</system.data>
...
</configuraiton>
请确保版本与您部署的Npgsql程序集的版本完全匹配(或忽略该版本/ Culture / PublicKeyToken)。 <清除/>
可以避免在运行 machine.config $中已经有Npgsql条目的计算机上运行时发生冲突。 c $ c>。没有清除,您将获得例外。但是,这还假设您不依赖
machine.config
中指定的任何其他提供程序作为应用程序。
Make sure the version matches exactly the version of the Npgsql assembly you deploy with (or just omit the Version/Culture/PublicKeyToken). The <Clear />
is there to avoid conflicts if running on a machine that already has entry for Npgsql in its machine.config
. Without the clear you would get an exception. However, that's also assuming you're not relying on any other providers specified in the machine.config
for your application.
这篇关于如何通过连接使用Entity Framework + PostgreSQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!