Gamemode development EF Core issues

Hello everyone

We used to play on a server that shut down, so now a friend and I are trying to create our own server. We are using C# as he has some prior knowledge, but we are facing some issues. We read most threads about EntityFramework on the forum together, and he helped me understand, but I still cannot fix the problem.

We are trying to save data to a SQL or MySQL server through the code. When we run our code with a typical project outside the server, it works fine, but when it runs through the server, it doesn’t work.

We used EntityFrameworkCore 2.2.6 with the SQL server, but we couldn’t make it work. So we switched to MySQL with the same version but including MySql.Data.EntityFrameworkCore 8.0.19… and that doesn’t work either.

Code
Context

	public class UserContext : DbContext
	{
		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			optionsBuilder.UseMySQL(@"Server=sql6.freesqldatabase.com;Initial Catalog=db;User ID=username; password=password;");
		}
		public UserContext() : base()
		{
	
		}
		// User
		public DbSet<User> Users { get; set; }	
		public DbSet<License> License { get; set; }
		public DbSet<Insurance> Insurance { get; set; }
		public DbSet<Profile> Profiles { get; set; }
	}

Fast test code

		[Command("register") ]
		public async Task Register()//string userName, string password)
		{
			var a = new UserContext();
			var b = a.Users.Add(new UserManagement.Entities.UserProfile.User()
			{
				Username = "HALLOFROMDB",
				Password = "HALLOFROMDB",
				Created = DateTime.UtcNow
			});
			await a.SaveChangesAsync();
			Debug.WriteLine("test");
}

I also tried to copy the dll files into the server output folder
SqlServerCodeFirst/Externals at master · XeroMS/SqlServerCodeFirst · GitHub (only the last 4)

The console doesn’t output much at first only

[ citizen-server-impl] server thread hitch warning: timer interval of 766 milliseconds

But after a good 3-4 mins idle it outputs (I did also try without multipleactiveresultsets, same error just without that line)

[ citizen-server-impl] server thread hitch warning: timer interval of 855 milliseconds
[script:FiveM.Serve] Unhandled task exception: 
[script:FiveM.Serve] System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception. ---> System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlConfiguration' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
[script:FiveM.Serve]   at System.Configuration.ClientConfigPaths..ctor (System.String exePath, System.Boolean includeUserConfig) [0x00050] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0
[script:FiveM.Serve]   at System.Configuration.ClientConfigPaths.GetPaths (System.String exePath, System.Boolean includeUserConfig) [0x00018] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0
[script:FiveM.Serve]   at System.Configuration.ClientConfigurationHost.get_ConfigPaths () [0x0000a] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0
[script:FiveM.Serve]   at System.Configuration.ClientConfigurationHost.GetStreamName (System.String configPath) [0x0006d] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0
[script:FiveM.Serve]   at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp () [0x00000] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0
[script:FiveM.Serve]   at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp () [0x00006] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0       
[script:FiveM.Serve]   at System.Configuration.ClientConfigurationSystem..ctor () [0x00051] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0
[script:FiveM.Serve]   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem () [0x00024] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0
[script:FiveM.Serve]    Exception_EndOfInnerExceptionStack
[script:FiveM.Serve]   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem () [0x00060] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0
[script:FiveM.Serve]   at System.Configuration.ConfigurationManager.PrepareConfigSystem () [0x0000a] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0
[script:FiveM.Serve]   at System.Configuration.ConfigurationManager.GetSection (System.String sectionName) [0x0000a] in <0e721f564ffa49e6b1d97ad7b9fda1f2>:0

[script:FiveM.Serve]   at MySql.Data.MySqlClient.MySqlConfiguration..cctor () [0x00000] in <27801dcf42584eb3a4768f5565b59758>:0
[script:FiveM.Serve]    Exception_EndOfInnerExceptionStack
[script:FiveM.Serve]   at MySql.Data.MySqlClient.Replication.ReplicationManager..cctor () [0x0001e] in <27801dcf42584eb3a4768f5565b59758>:0
[script:FiveM.Serve]    Exception_EndOfInnerExceptionStack
[script:FiveM.Serve]   at MySql.Data.MySqlClient.MySqlConnection.Open () [0x002be] in <27801dcf42584eb3a4768f5565b59758>:0
[script:FiveM.Serve]   at System.Data.Common.DbConnection.OpenAsync (System.Threading.CancellationToken cancellationToken) [0x00011] in <fbc671365aec480fa83af18ec11a08da>:0
[script:FiveM.Serve] --- End of stack trace from previous location where exception was thrown ---
[script:FiveM.Serve]
[script:FiveM.Serve]   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync (System.Boolean errorsExpected, System.Threading.CancellationToken cancellationToken) [0x000f9] in <8e5c3df7a4c34b58a9c763fcfeecdb4f>:0
[script:FiveM.Serve]   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync (System.Threading.CancellationToken cancellationToken, System.Boolean errorsExpected) [0x0009b] in <8e5c3df7a4c34b58a9c763fcfeecdb4f>:0
[script:FiveM.Serve]   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionAsync (System.Data.IsolationLevel isolationLevel, System.Threading.CancellationToken cancellationToken) [0x00069] in <8e5c3df7a4c34b58a9c763fcfeecdb4f>:0
[script:FiveM.Serve]   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransactionAsync (System.Threading.CancellationToken cancellationToken) [0x00069] in <8e5c3df7a4c34b58a9c763fcfeecdb4f>:0
[script:FiveM.Serve]   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync (Microsoft.EntityFrameworkCore.DbContext _, System.ValueTuple`2[T1,T2] parameters, System.Threading.CancellationToken cancellationToken) [0x00110] in <8e5c3df7a4c34b58a9c763fcfeecdb4f>:0
[script:FiveM.Serve]   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync (System.Collections.Generic.IReadOnlyList`1[T] entriesToSave, System.Threading.CancellationToken cancellationToken) [0x00088] in <6bcdfaaeeef5425b99b4bfde66161a46>:0
[script:FiveM.Serve]   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync (System.Boolean acceptAllChangesOnSuccess, System.Threading.CancellationToken cancellationToken) [0x000f8] in <6bcdfaaeeef5425b99b4bfde66161a46>:0
[script:FiveM.Serve]   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync (System.Boolean acceptAllChangesOnSuccess, System.Threading.CancellationToken cancellationToken) [0x000e9] in <6bcdfaaeeef5425b99b4bfde66161a46>:0
[script:FiveM.Serve]   at FiveM.Server.User.UserManagement.Register () [0x00083] in C:\udv\Server\src\gamemode\.FiveM\Server\User\UserManagement.cs:31

Anyone know how to use Entityframework core with FiveM. I know some people made it work but I have no idea what they have done to do so.

Instead of Oracle’s MySql.Data, try using GitHub - mysql-net/MySqlConnector: Async MySQL Connector for .NET and .NET Core.

1 Like

Hello d-bubble

Thank you very much for your response. It works like a charm.

I guess we will be using MySQL, as it seems like we are unable to make it work with Microsoft SQL server.

Thanks

For what it’s worth, Npgsql (for Postgres) also seems to work fine on this Mono version’s netstandard2.0 binaries. MSSQL libraries I think were a lot more picky as they used to be built in to ‘classic’ .NET.