How to fix `An existing connection was forcibly closed by the remote host` error in EF Core

0

I'm changing my database server provider from MySQL to SQL Server Express, so I'm changing all my .NET Framework 4.6.1 class libraries that I used before for MySQL database connection. Of course I re-created the migration, install Microsoft.EntityFrameWorkCore.SqlServer instead of the MySQL one and set up the DbContext.

I use these libraries as a game server plugins, so I installed them there. Sadly if fails to load and throws this error:

System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host.

I tried running the same DbContext as .NET Framework 4.6.1 console application and it worked. I also changed everything up for my other class library which was using MySQL, but it also failed to load. So I believe this error happens only to class library.

My database context (SQLKits is the name of my game plugin)

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;");
    }
}

and this is an image of my instance class where I define DbContext:

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

I also tried to use it without migration, but it failed either.

And in SQL Server Profiler there's nothing about the connection try, so plugin doesn't even connect to it.

I'm debugging this issue for a long time and I really couldn't solve it myself nor nobody could help me, so that's why I'm asking for solution here.

~Thank you

Full Error:

System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host.

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

--- End of stack trace from previous location where exception was thrown ---

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

c#
sql-server
entity-framework-core
asked on Stack Overflow Apr 19, 2019 by MCrow • edited Apr 19, 2019 by marc_s

1 Answer

0

Change your connection string to

optionsBuilder.UseSqlServer(@"Server=.\sqlexpress;Database=unturned;Trusted_Connection=True;")
answered on Stack Overflow Apr 20, 2019 by ErikEJ

User contributions licensed under CC BY-SA 3.0