如何修复`EF Core中的远程主机`错误强制关闭现有连接

问题描述 投票:0回答:1

我正在将我的数据库服务器提供程序从MySQL更改为SQL Server Express,因此我正在更改以前用于MySQL数据库连接的所有.NET Framework 4.6.1类库。当然我重新创建了迁移,安装Microsoft.EntityFrameWorkCore.SqlServer而不是MySQL,并设置DbContext

我使用这些库作为游戏服务器插件,所以我在那里安装它们。遗憾的是,如果加载失败并抛出此错误:

System.Net.Sockets.SocketException(0x80004005):远程主机强制关闭现有连接。

我尝试运行与.NET Framework 4.6.1控制台应用程序相同的DbContext,它工作正常。我也为我使用MySQL的其他类库改变了一切,但它也无法加载。所以我相信这个错误只发生在类库中。

我的数据库上下文(SQLKits是我的游戏插件的名称)

public class SQLKitsContext : DbContext
{
    public virtual DbSet<Kit> Kits { get; set; }
    public virtual DbSet<KitItem> KitItems { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=localhost\sqlexpress;Database=unturned;Trusted_Connection=True;");
    }
}

这是我的实例类的图像,我在其中定义DbContext

https://i.imgur.com/hb0H9l0.png

我也试图在没有迁移的情况下使用它,但它也失败了。

在SQL Server Profiler中没有任何关于连接尝试,所以插件甚至没有连接到它。

我正在调试这个问题很长一段时间,我自己也无法解决,也没有人能帮助我,所以这就是我在这里要求解决方案的原因。

〜谢谢

完全错误:

System.Net.Sockets.SocketException(0x80004005):远程主机强制关闭现有连接。

在Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute [TState,TResult](Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy strategy,System.Func2[T,TResult] operation, System.Func2 [T,TResult] verifySucceeded,TState state)[0x0001f] in:0 在Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute [TState,TResult](Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy strategy,TState state,System.Func`2 [T,TResult] operation)[0x00000] in:0 在<253b972c331e4e9c86e8f6b8430dc9d0>中的Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(System.Boolean retryOnNotExists)[0x00034]:0 在<253b972c331e4e9c86e8f6b8430dc9d0>中的Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()[0x00000]:0 at <69f795dffc844780bfcfff4ff8415a92>中的Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()[0x0000b]:0 at <69f795dffc844780bfcfff4ff8415a92>中的Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(System.String targetMigration)[0x00012]:0 在<69f795dffc844780bfcfff4ff8415a92>中的Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade databaseFacade)[0x00010]:0 在RestoreMonarchy.SQLKits.SQLKitsPlugin + d__13.MoveNext()[0x00033] in <4589f5df884b435cab61ef028aadd6e8>:0

---从抛出异常的先前位置开始的堆栈跟踪结束---

在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] in:0 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任务)[0x0003e] in:0 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任务)[0x00028] in:0 在System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task任务)[0x00008] in:0 在System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000]中:0 在Rocket.Core.Plugins.Plugin + d__32.MoveNext()[0x002c4] in <4bcff08a1274468caf2867ee950c3ee7>:0```

c# sql-server entity-framework-core
1个回答
0
投票

将您的连接字符串更改为

optionsBuilder.UseSqlServer(@"Server=.\sqlexpress;Database=unturned;Trusted_Connection=True;")
© www.soinside.com 2019 - 2024. All rights reserved.