是否可以在不使用machine.config的情况下为migrate.exe配置自定义提供程序 [英] Is it possible to configure a custom provider for migrate.exe without using the machine.config

查看:126
本文介绍了是否可以在不使用machine.config的情况下为migrate.exe配置自定义提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用EntityFramework v6 nuget软件包中提供的migration.exe设置自动迁移.我的应用程序使用PostgreSQL数据库并使用自定义数据库提供程序(Npgsql),这需要对EntityFramework进行其他配置.

I am trying to setup automated migrations using migrate.exe that is provided in the EntityFramework v6 nuget package. My application uses a PostgreSQL database and uses a custom database provider (Npgsql), which requires additional configuration for EntityFramework.

Migrate.exe将成功运行,但是,这不是一个实际的解决方案,因为它要求在运行迁移的所有地方都编辑machine.config.我也想在AppVeyor中将其设置为构建过程的一部分,但我认为无法编辑AppVeyor构建服务器的machine.config.

Migrate.exe works successfully if I add the required configuration into the machine.config, however this is not a practical solution as it requires editing the machine.config everywhere the migrations are run. I also want to set this up as part of a build process in AppVeyor and I don’t think it is possible to edit the machine.config of an AppVeyor build server.

作为编辑machine.config的一种替代方法,我尝试创建一个包含migration.exe以及所需配置的migration.exe.config文件,但这不起作用,并导致以下错误.

As an alternative to editing the machine.config I tried creating a migrate.exe.config file containing the required configuration alongside migrate.exe, but this does not work and results in the following error.

错误:变量名称为'Npgsql'的ADO.NET提供程序未在计算机或应用程序配置文件中注册,或者无法加载.有关详细信息,请参见内部异常.

ERROR: The ADO.NET provider with invariant name 'Npgsql' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.

是否有另一种方法可以在不使用machine.config的情况下为migration.exe配置自定义提供程序?

Is there another way to configure a custom provider for migrate.exe without using the machine.config?

作为参考,所需的其他配置是

For reference the additional configuration required is

<system.data>
  <DbProviderFactories>
    <remove invariant="Npgsql" />
    <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
  </DbProviderFactories>
</system.data>
<entityFramework>
  <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql" />
  <providers>
    <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
  </providers>
</entityFramework>

推荐答案

我刚刚经历了此过程(并非专门针对AppVeyor,但我正在努力实现TeamCity构建服务器的自动化),并且能够使用Entity使其正常工作框架6.0.0.0.这是我运行的命令,它导致了相同的错误

I just went through this (not specifically with AppVeyor, but I'm working towards automating my TeamCity build server) and was able to get it to work using Entity Framework 6.0.0.0. Here's the command I was running which resulted in the same error

.\migrate.exe
  MyProject.DataContracts.dll
  /ConnectionString:"server=localhost;user id=<myuserid>;password=<mypassword>;database=<mydatabase>"
  /connectionProviderName:Npgsql

缺少的组件正在为我的启动项目指定配置文件,

The missing component was specifying the config file for my startup project, like so

.\migrate.exe
  MyProject.DataContracts.dll
  /startupConfigurationFile:"..\..\..\MyStartupProject\Web.config"
  /ConnectionString:"server=localhost;user id=<myuserid>;password=<mypassword>;database=<mydatabase>"
  /connectionProviderName:Npgsql

作为参考,这是我的Web.config

<system.data>
  <DbProviderFactories>
    <remove invariant="Npgsql" />
    <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
  </DbProviderFactories>
</system.data>

<entityFramework>
  <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql.EntityFramework" />
  <providers>
    <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework"/>
  </providers>
</entityFramework>

希望这对那些不希望数据库自动初始化的人有所帮助,如另一个答案所示.干杯!

Hopefully this helps anyone landing on this question who doesn't want automatic database initialization as shown in the other answer. Cheers!

这篇关于是否可以在不使用machine.config的情况下为migrate.exe配置自定义提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆