如何禁用MARS并规避“尚未实施的MARS" -exception? [英] How to disable MARS and circumvent "MARS is not yet implemented"-exception"?

查看:89
本文介绍了如何禁用MARS并规避“尚未实施的MARS" -exception?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用包 Npsql 将PostgreSQL数据库与Entity Framework on Mono一起使用时和 Npsql.EntityFramework 我在尝试从控制台应用程序.该连接确实可以在应用程序中正常工作,并且可以通过编程对数据库进行CRUD.

While using a PostgreSQL database with Entity Framework on Mono using the packages Npsql and Npsql.EntityFramework I get an exception while trying to run Code First migrations from a Console app. The connection does work in the app and the database can be CRUD'ed programmatically.

Context类的外观如下:

public class ZkContext : DbContext, IZkContext
{

    public ZkContext() : base("ZkTestDatabaseConnection")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // PostgreSQL uses schema public by default.
        modelBuilder.HasDefaultSchema("public");

    }

    public IDbSet<Crop> Crops { get; set; } 

}

此外,还有一个类Configuration,它从DbMigrationsConfiguration<T>派生如下:

Furthermore, there is a class Configuration that derives from DbMigrationsConfiguration<T> as follows:

public class Configuration : DbMigrationsConfiguration<ZkContext>
{
    public Configuration ()
    {
        AutomaticMigrationsEnabled = false;
        SetSqlGenerator("Npgsql", new PostgreSqlMigrationSqlGenerator());
    }
}

PostgreSqlMigrationSqlGenerator类来自此PostgreSqlMigrationSqlGenerator存储库(相同的错误会在默认情况下弹出SqlGenerator,因此代码不是问题.)

The PostgreSqlMigrationSqlGenerator class comes from this PostgreSqlMigrationSqlGenerator repository (the same error pops up with the default SqlGenerator so that code is not the problem).

我尝试通过此想法从控制台应用程序运行配置,如下所示,这是可能的,因为PowerShell命令只是底层API的薄包装":

I try to run the configuration from a console app as follows via this idea, which is possible since the PowerShell commands "are just thin wrappers over an underlying API":

var config = new Configuration();
var scaffolder = new MigrationScaffolder(config); //<= Here it breaks
var migration = scaffolder.Scaffold("Initial");

不幸的是,添加MigrationScaffolder(config)语句,此错误会弹出:

Unfortunately, add the MigrationScaffolder(config) statement this error pops up:

System.NotImplementedException已引发.火星尚未实施!

System.NotImplementedException has been thrown. MARS is not yet implemented!

多个活动结果集(MARS)显然尚未在Mono中实现.类 负责.如果您单击该代码的链接,则可以看到在line 797上引发了异常:

Multiple Active Result Sets (MARS) are apparently not implemented in Mono yet. The class System.Data.SqlClient.SqlConnectionStringBuilder in the Mono framework is responsible. If you follow the link to the code you can see that on line 797 the exception is thrown:

case "MULTIPLEACTIVERESULTSETS":
    if (value == null) {
        _multipleActiveResultSets = DEF_MULTIPLEACTIVERESULTSETS;
        base.Remove (mappedKey);
    } else if ( DbConnectionStringBuilderHelper.ConvertToBoolean (value))
        throw new NotImplementedException ("MARS is not yet implemented!");
    break;

SetValue (string key, object value)方法中.

我的问题是:有没有办法推迟MARS并让Migration Generator不引发异常?

/edit将;MultipleActiveResultSets=False附加到连接字符串无济于事,因为它不是PostgreSQL连接字符串的有效属性.此外,在ZkContext上下文类上设置Configuration.LazyLoadingEnabled = false;也无济于事.

/edit Appending ;MultipleActiveResultSets=False to the connection string does not help since it is not a valid property for PostgreSQL connection strings. Furthermore, setting Configuration.LazyLoadingEnabled = false; on the ZkContext context class does not help either.

/edit调用堆栈:

System.Data.SqlClient.SqlConnectionStringBuilder.SetValue(key ="multipleactiveresultsets",value ="True")在 System.Data.SqlClient.SqlConnectionStringBuilder.set_Item(keyword ="multipleactiveresultsets",value ="True")在
中的System.Data.Common.DbConnectionStringBuilder.ParseConnectionStringNonOdbc(connectionString ="Data Source =.\ SQLEXPRESS; Integrated Security = True; MultipleActiveResultSets = True;")
中的System.Data.Common.DbConnectionStringBuilder.ParseConnectionString(connectionString ="数据
Source =.\ SQLEXPRESS; Integrated Security = True; MultipleActiveResultSets = True;"
)
中的System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(value ="Data
Source =.\ SQLEXPRESS; Integrated Security = True; MultipleActiveResultSets = True;")
中的System.Data.SqlClient.SqlConnectionStringBuilder..ctor(connectionString ="Data
Source =.\ SQLEXPRESS; Integrated Security = True; MultipleActiveResultSets = True;") System.Data.Entity.Infrastructure.SqlConnectionFactory.CreateConnection
(
中的(nameOrConnectionString ="ZkTestDatabaseConnection")
中的System.Data.Entity.Internal.LazyInternalConnection.Initialize()
中的System.Data.Entity.Internal.LazyInternalConnection.get_Connection()
中的System.Data.Entity.Internal.LazyInternalContext.get_Connection() System.Data.Entity.Infrastructure.DbContextInfo..ctor(contextType = {Zk.Models.ZkContext},
modelProviderInfo =(null),config = {System.Data.Entity.Internal.AppConfig},
connectionInfo =
中的System.Data.Entity.Infrastructure.DbContextInfo..ctor(contextType = {Zk.Models.ZkContext},
resolver =(null))
中的System.Data.Entity.Infrastructure.DbContextInfo..ctor(contextType = {Zk.Models.ZkContext}) System.Data.Entity.Migrations.DbMigrator..ctor(配置=
{Zk.Migrations.Configuration},usersContext =(空),
existenceState = System.Data.Entity.Internal.DatabaseExistenceState.Unknown,

中称为"ByCreateDatabase = false"
中的System.Data.Entity.Migrations.DbMigrator..ctor(配置=
{Zk.Migrations.Configuration})
中的System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(migrationsConfiguration =
{Zk.Migrations.Configuration}) /home/erwin/zaaikalender
/Zk.Migrations/MigrationsTool.cs:23

System.Data.SqlClient.SqlConnectionStringBuilder.SetValue (key="multipleactiveresultsets", value="True") in System.Data.SqlClient.SqlConnectionStringBuilder.set_Item (keyword="multipleactiveresultsets", value="True") in System.Data.Common.DbConnectionStringBuilder.ParseConnectionStringNonOdbc (connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True;") in
System.Data.Common.DbConnectionStringBuilder.ParseConnectionString (connectionString="Data
Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True;"
) in
System.Data.Common.DbConnectionStringBuilder.set_ConnectionString (value="Data
Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True;") in
System.Data.SqlClient.SqlConnectionStringBuilder..ctor (connectionString="Data
Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True;") in
System.Data.Entity.Infrastructure.SqlConnectionFactory.CreateConnection
(nameOrConnectionString="ZkTestDatabaseConnection") in
System.Data.Entity.Internal.LazyInternalConnection.Initialize () in
System.Data.Entity.Internal.LazyInternalConnection.get_Connection () in
System.Data.Entity.Internal.LazyInternalContext.get_Connection () in
System.Data.Entity.Infrastructure.DbContextInfo..ctor (contextType={Zk.Models.ZkContext},
modelProviderInfo=(null), config={System.Data.Entity.Internal.AppConfig},
connectionInfo=(null), resolver=(null)) in
System.Data.Entity.Infrastructure.DbContextInfo..ctor (contextType={Zk.Models.ZkContext},
resolver=(null)) in
System.Data.Entity.Infrastructure.DbContextInfo..ctor (contextType={Zk.Models.ZkContext}) in
System.Data.Entity.Migrations.DbMigrator..ctor (configuration=
{Zk.Migrations.Configuration}, usersContext=(null),
existenceState=System.Data.Entity.Internal.DatabaseExistenceState.Unknown,
calledByCreateDatabase=false) in
System.Data.Entity.Migrations.DbMigrator..ctor (configuration=
{Zk.Migrations.Configuration}) in
System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor (migrationsConfiguration=
{Zk.Migrations.Configuration}) in
Zk.Migrations.MigrationsTool.Main (args={string[0]}) in /home/erwin/zaaikalender
/Zk.Migrations/MigrationsTool.cs:23

粗体连接字符串不是指定的连接字符串.

The bold connection string is not the specified connection string.

推荐答案

没有必要禁用MARS.从显式堆栈跟踪中可以看到,我的上下文使用了默认的连接字符串.发生这种情况是因为我正在从控制台应用程序中的单独项目运行迁移.

It was not necessary to disable MARS. What can be seen from the explicit stack trace is that my context used a default connection string. This happened because I was running migrations from a separate project in a console app.

当我将默认项目(DbContext所在的地方)的web.config中的某些信息复制到构建器编译的控制台应用程序的app.config中时.

When I copied some of the information in the web.config of the default project (where the DbContext resided) to the app.config of the console app the builder compiled.

迁移正常(!),并且由于现在采用了正确的连接字符串,因此不再发生MARS错误.

The migrations are working (!) and the MARS error does not happen any more since the correct connection string is now taken.

迁移项目app.config中的xml配置重复:

Duplicated xml-configuration in app.config of migrations project:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" 
             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.1.1, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
             requirePermission="false" />
  </configSections>
  <!-- <connectionStrings configSource="../Zk/ConnectionStrings.config" /> -->
  <connectionStrings>
    <clear />
    <add name="ZkTestDatabaseConnection" 
         connectionString="Server=localhost;Port=5432;Database=ZkTestDatabase;User Id=zktest;Password=broccoli;CommandTimeout=20;" 
         providerName="Npgsql" />
  </connectionStrings>
  <system.data>
    <DbProviderFactories>
      <add name="Npgsql Data Provider" 
           invariant="Npgsql" 
           description="Data Provider for PostgreSQL" 
           type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />
    <providers>
      <provider invariantName="Npgsql" 
                type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
    </providers>
  </entityFramework>
</configuration> 

这篇关于如何禁用MARS并规避“尚未实施的MARS" -exception?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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