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

1

0

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://learn.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?

Ankara answered 27/12, 2020 at 11:5 Comment(0)
B
0

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.

Binford answered 27/12, 2020 at 12:42 Comment(1)
My application is targeting to .NET 5. I have also other MVC, targeting to .NET 4.8. In this case it works. If called from .NET 5, Npgsql must registered from code to make it work in .NET 5.Ankara

© 2022 - 2024 — McMap. All rights reserved.