gpt4 book ai didi

c# - 如何禁用 MARS 并规避 "MARS is not yet implemented"-exception”?

转载 作者:行者123 更新时间:2023-11-29 11:47:51 27 4
gpt4 key购买 nike

在使用包 Npsql 在 Mono 上使用带有 Entity Framework 的 PostgreSQL 数据库时和 Npsql.EntityFramework 尝试从控制台应用程序运行 Code First 迁移时出现异常。该连接在应用程序中确实有效,并且可以通过编程方式对数据库进行 CRUD。

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>如下:

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

PostgreSqlMigrationSqlGenerator类来自 this PostgreSqlMigrationSqlGenerator repository (默认的 SqlGenerator 会弹出相同的错误,因此代码不是问题)。

我尝试通过 this idea 从控制台应用程序运行配置,如下所示,这是可能的,因为 PowerShell 命令“只是对底层 API 的简单包装”:

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

不幸的是,添加 MigrationScaffolder(config)声明此错误弹出:

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

Multiple Active Result Sets (MARS) 显然还没有在 Mono 中实现。类(class) System.Data.SqlClient.SqlConnectionStringBuilder 在 Mono 框架中负责。如果您点击代码链接,您可以在 line 797 上看到它抛出异常:

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 并让迁移生成器不抛出异常?

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

/编辑调用堆栈:

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

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

最佳答案

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

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

迁移正在运行(!)并且 MARS 错误不再发生,因为现在采用了正确的连接字符串。

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

<?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>

关于c# - 如何禁用 MARS 并规避 "MARS is not yet implemented"-exception”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27069403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com