问题
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