Webster.

[C#] Entity Framework error

Recommended Posts

I'm trying to add a SQL Server Database to my RAGE server, using Entity Framework Core.

I've created a resource and a separate project (in the same solution) for the database, getting to the following folder structure:

Quote
  • resources (Solution)
    • Database (Project)
      • Entities
        • Account.cs
      • ServerDbContext.cs
    • LoginRegister (Project)
      • Main.cs
      • meta.xml

 

and my Main.cs looks like this:

using Database;
using GTANetworkAPI;
using System;

namespace LoginRegister
{
    public class Main : Script
    {
        [ServerEvent(Event.ResourceStart)]
        public void EventOnResourceStart()
        {
            try
            {
                using var context = new ServerDbContext();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR!");
            }
        }
    }
}

And when I run server.exe, I get the following error in the console (which disappears when I remove the DbContext creation part from EventOnResourceStart:

Quote

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.PlatformNotSupportedException: Microsoft.Data.SqlClient is not supported on this platform.
   at new Microsoft.Data.SqlClient.SqlConnection(string connectionString)
   at DbConnection Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
   at DbConnection Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at DbCommand Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
   at RelationalDataReader Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at bool Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor+QueryingEnumerable<T>+Enumerator.MoveNext()
   at TSource System.Linq.Enumerable.Single<TSource>(IEnumerable<TSource> source)
   at TResult Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<TResult>(Expression query)
   at TResult Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<TResult>(Expression expression)
   at int System.Linq.Queryable.Count<TSource>(IQueryable<TSource> source)
   at new TestResource.Main() in D:\Games\RageMP\server-files\bridge\resources\TestResource\Main.cs:line 15
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor)
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at GTANetworkInternals.GameServer.InstantiateScripts(Assembly targetAssembly, String resourceName)+MoveNext()
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at GTANetworkInternals.GameServer.StartResource(String resourceName, String parent)

 

 

 

 

 

Edited by Webster.

Share this post


Link to post
Share on other sites

I've posted a message on the discord server and followed the https://discordapp.com/channels/183979885788659713/379670365061775360/615468230294896640 tutorial, as instructed.

Now I can actually create the db context, but I get an error when trying to use it:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.PlatformNotSupportedException: Microsoft.Data.SqlClient is not supported on this platform.
   at new Microsoft.Data.SqlClient.SqlConnection(string connectionString)
   at DbConnection Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
   at DbConnection Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
   at DbCommand Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
   at RelationalDataReader Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at bool Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor+QueryingEnumerable<T>+Enumerator.MoveNext()
   at TSource System.Linq.Enumerable.Single<TSource>(IEnumerable<TSource> source)
   at TResult Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<TResult>(Expression query)
   at TResult Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<TResult>(Expression expression)
   at int System.Linq.Queryable.Count<TSource>(IQueryable<TSource> source)
   at new TestResource.Main() in D:\Games\RageMP\server-files\bridge\resources\TestResource\Main.cs:line 15
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor)
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at GTANetworkInternals.GameServer.InstantiateScripts(Assembly targetAssembly, String resourceName)+MoveNext()
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at GTANetworkInternals.GameServer.StartResource(String resourceName, String parent)
using Database;
using GTANetworkAPI;
using System;
using System.Linq;

namespace TestResource
{
    public class Main : Script
    {
        public Main()
        {
            Console.WriteLine("Hello World");
            using (var dbContext = new ServerDbContext())
            {
                Console.WriteLine($"There are {dbContext.Accounts.Count()} accounts in the database!");
            }
        }
    }
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.