是否有可能摆脱 app.config 依赖?

Is it possible to get rid of app.config depedency?

我们将 .Net Framework 4.5 项目升级到 .Net Framework 4.8,我们的 app.config 文件中出现了一些新内容。

MySQL 和 EntityFramework 等是否需要下面的代码(来自 app.config)才能正常工作?

此外,是否可以通过编程方式完成与以下代码相同的事情,以便我们摆脱 app.config 依赖项?

我们试图只删除下面的代码,但这导致我们的服务无法启动。

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>
  </entityFramework>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data" />
    </DbProviderFactories>
  </system.data>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=<PUBLICKEY>" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=<PUBLICKEY>" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>

对于EntityFramework

Microsoft 文档上有一个特定的指南,说明从 EF6 开始可以完成。检查 here

有效的步骤如下:

  1. 只为您的应用程序创建一个 DbConfiguration class。此 class 指定应用程序域范围的设置。
  2. 将您的 DbConfiguration class 放在与 DbContext class 相同的程序集中。 (如果您想更改此设置,请参阅移动 DbConfiguration 部分。)
  3. 为您的 DbConfiguration class 提供一个 public 无参数构造函数。
  4. 通过从此构造函数中调用受保护的 DbConfiguration 方法来设置配置选项。

示例:

[DbConfigurationType(typeof(MyDbConfiguration))]
public class MyContextContext : DbContext
{
}

对于支持的运行时

这已经涵盖了:

What happens if I remove the auto added supportedRuntime element?