在保持现有SQL Server提供商完整的同时,如何将PostgreSQL添加到现有实体框架6项目中?
免责声明:我是一个初中的开发人员,仍在试图将我的头缠绕在EF6周围。
我们的团队有一个.NET Framework 4.7 / Entity Framework 6项目,该项目依赖于MS SQL Server的数据。我们只是升级到Visual Studio 2022,但似乎有些推荐的工具对VS2022不起作用。我们首先将数据库用于EF。
最近,我们的团队决定添加一个新的PostgreSQL数据库,我们需要在现有项目中添加对其的支持。
我已经安装了以下软件包:
- NPGSQL
- ENTITYFRAMEWORK6.NPGSQL
我还被建议安装以下VS扩展名:
- NPGSQL PostgreSQL Integration
,但似乎它仅兼容VS2019,并已被放弃。
为了测试是否可以使用推荐的VS扩展程序进行测试,我使用了Visual Studio 2019而不是2022。
在安装上面列出的项目后,我的Web.config文件现在具有此EntityFramework配置:
<entityFramework>
<defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.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>
和此系统。数据配置:
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql" />
<add name="Npgsql Provider" invariant="Npgsql" description=".NET Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=4.1.3.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</DbProviderFactories>
</system.data>
在这一点上,我尝试创建一个新的ado.net实体数据模型
,我能够选择所需的表格和过程。
这就是我被卡住的地方。
在我的web.config中添加了一个新的ConnectionsTring,以及我们已经用于SQL Server的加密连接串。测试时,会引发一个异常,说我不能在加密的连接串旁边有一个未加密的连接串。
我尝试将ConnectionsTring放置在代码中,但然后在解析ConnectionsTring时会引发错误,称其不识别参数。我使用了自动生成的连接串中相同的参数。
因此,似乎正在使用SQL Server提供商而不是PostgreSQL提供商读取Postgres ConnectionsTring。
有什么建议吗?还是我可以用来帮助我的资源?提前致谢。
Disclaimer: I'm a junior dev and still trying to wrap my head around EF6.
Our team has a .NET Framework 4.7 / Entity Framework 6 project which relies on MS SQL Server for its data. We just upgraded to Visual Studio 2022 but it seems like some of the recommended tools don't work on VS2022. We use database first for EF.
Recently, our team decided to add a create a new PostgreSQL database and we need to add support for it in our existing project.
I've installed the following packages:
- Npgsql
- EntityFramework6.Npgsql
I was advised to also install the following VS extension:
- Npgsql PostgreSQL Integration
But it seems like it is only compatible up to VS2019 and has been abandoned.
In order to test if I could even set this up at all with the recommended VS extension, I used Visual Studio 2019 instead of 2022.
After installing the items listed above, my web.config file now has this entityFramework config:
<entityFramework>
<defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.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>
and this system.data config:
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql" />
<add name="Npgsql Provider" invariant="Npgsql" description=".NET Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=4.1.3.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</DbProviderFactories>
</system.data>
At this point, I attempted creating a new ADO.NET Entity Data Model
, I was able to choose the table and procedure I wanted.
This is where I got stuck.
A new connectionString was added to my web.config, alongside the encrypted connectionString we were already using for SQL Server. When testing, it throws an exception saying that I cannot have an unencrypted connectionString next to an encrypted connectionString.
I tried placing the connectionString within the code but then it throws errors when parsing the connectionString, saying that it does not recognize the parameters. I used the same parameters from the connectionString that was automatically generated.
So, it seems like it's reading the Postgres connectionString with the SQL Server provider instead of the PostgreSQL provider.
Any suggestions? Or resources I can use to help me along the way? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的持久性层中必须是配置(必须创建),第二个必须将连接字符串添加到appsettings.json ...然后,当您想要的时候,您可以更改数据库..
for exmample
我的应用程序设置
和 。 .cs在持久性层中,
我希望这对您有帮助。
in your Persistence Layer must be Configurations (you must create) and for second you must add Connection strings to appsettings.json ... then when you want you can change you database..
for exmample
and my app settings.json
and for end DesignTimeDbContextFactory.cs in Persistence layer
I hope this will help you..