How to use WebMatrix methods with Npgsql in .NET MVC Core 5

给你一囗甜甜゛ 提交于 2021-01-07 04:31:13

问题


I want to use WebMatrix.Data namespace

#region Assembly WebMatrix.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// .. \packages\Microsoft.AspNet.WebPages.Data.3.2.7\lib\net45\WebMatrix.Data.dll

with Npgsql data provider in ASP MVC Core 5.

In web.config it is defined as

<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" />
  </DbProviderFactories>
</system.data>

.NET 5 does not read config from web.config. I tried according to

Add a DbProviderFactory without an App.Config

in code

System.Data.Common.DbProviderFactories.RegisterFactory("NpgSql", Npgsql.NpgsqlFactory.Instance);

but got compile error RegisterFactory method does not exist. VS2019 assembly viewer confirms this:

#region Assembly System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.Data.dll
#endregion

namespace System.Data.Common
{
    public static class DbProviderFactories
    {
        public static DbProviderFactory GetFactory(string providerInvariantName);
        public static DbProviderFactory GetFactory(DataRow providerRow);
        public static DbProviderFactory GetFactory(DbConnection connection);
        public static DataTable GetFactoryClasses();
    }
}

According to doc it must exist:

https://docs.microsoft.com/en-us/dotnet/api/system.data.common.dbproviderfactories.registerfactory?view=net-5.0#System_Data_Common_DbProviderFactories_RegisterFactory_System_String_System_Data_Common_DbProviderFactory_

How to use Postgres database in WebMatrix in .NET MVC Core 5?


回答1:


It seems you are targeting .NET Framework 4.8, not .NET 5:

// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.Data.dll

Also, WebMatrix is an old technology which has been discontinued and whose formal support has ended on November 1st, 2017. So it is unlikely that you will get it working on .NET 5.



来源:https://stackoverflow.com/questions/65465068/how-to-use-webmatrix-methods-with-npgsql-in-net-mvc-core-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!