Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

EntityFramework works in Editor crashes when ran from a build

Discussion in 'Windows' started by GambitMonkey, Mar 20, 2022.

  1. GambitMonkey

    GambitMonkey

    Joined:
    Apr 5, 2016
    Posts:
    65
    Hello everyone.

    We are working on our database code and are PoCing EntityFramework in Unity.

    The code runs in the Editor and builds for both IL2CPP and Mono 2.x, but when launching the player we get the following error.

    Code (CSharp):
    1. InvalidOperationException: A suitable constructor for type 'Microsoft.EntityFrameworkCore.Internal.DbSetInitializer' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.
    2.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite (Microsoft.Extensions.DependencyInjection.ServiceLookup.ResultCache lifetime, System.Type serviceType, System.Type implementationType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) [0x00000] in <00000000000000000000000000000000>:0
    3.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact (Microsoft.Extensions.DependencyInjection.ServiceDescriptor descriptor, System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain, System.Int32 slot) [0x00000] in <00000000000000000000000000000000>:0
    4.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) [0x00000] in <00000000000000000000000000000000>:0
    5.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) [0x00000] in <00000000000000000000000000000000>:0
    6.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory+<>c__DisplayClass7_0.<GetCallSite>b__0 (System.Type type) [0x00000] in <00000000000000000000000000000000>:0
    7.   at System.Func`2[T,TResult].Invoke (T arg) [0x00000] in <00000000000000000000000000000000>:0
    8.   at System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) [0x00000] in <00000000000000000000000000000000>:0
    9.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) [0x00000] in <00000000000000000000000000000000>:0
    10.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor (System.Type serviceType) [0x00000] in <00000000000000000000000000000000>:0
    11.   at System.Func`2[T,TResult].Invoke (T arg) [0x00000] in <00000000000000000000000000000000>:0
    12.   at System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) [0x00000] in <00000000000000000000000000000000>:0
    13.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope serviceProviderEngineScope) [0x00000] in <00000000000000000000000000000000>:0
    14.   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService (System.Type serviceType) [0x00000] in <00000000000000000000000000000000>:0
    15.   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService (System.Type serviceType) [0x00000] in <00000000000000000000000000000000>:0
    16.   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService (System.IServiceProvider provider, System.Type serviceType) [0x00000] in <00000000000000000000000000000000>:0
    17.   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T] (System.IServiceProvider provider) [0x00000] in <00000000000000000000000000000000>:0
    18.   at Microsoft.EntityFrameworkCore.DbContext..ctor (Microsoft.EntityFrameworkCore.DbContextOptions options) [0x00000] in <00000000000000000000000000000000>:0
    19.   at Microsoft.EntityFrameworkCore.DbContext..ctor () [0x00000] in <00000000000000000000000000000000>:0
    20.   at AhBearStudios.Database.SQLiteDbContext..ctor () [0x00000] in <00000000000000000000000000000000>:0
    21.   at SampleDB.Start () [0x00000] in <00000000000000000000000000000000>:0
    To recreate this issue simply import the attached package. Create a Empty Game object -> Attach the SampleDB script -> Build -> Run

    Thanks in advanced!
     

    Attached Files:

  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,438
    It looks like it is trying to access some types/members via reflection, without there being any explicit references to them. When using IL2CPP, Unity will remove unused code, and if the code is only used through reflection, then the reflection will not be able to find it. See https://docs.unity3d.com/Manual/ManagedCodeStripping.html for more details.
     
  3. GambitMonkey

    GambitMonkey

    Joined:
    Apr 5, 2016
    Posts:
    65
    Tautvydas,
    Thank you for your response. I will look further into the reflection issue, but this also happens when running it built against Mono 2.x which I did not think reflection was an issue when using Mono.

    Appreciate the help.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,438
    It has the same limitation if stripping is enabled in player settings.
     
  5. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    I'm facing exactly the same issue. Could you resolve it by deactivating code stripping? And if so, is it possible to work around this without changing the project settings?
     
  6. demendez

    demendez

    Joined:
    Jan 26, 2021
    Posts:
    1
    I have a similar issue with IL2CPP ( InvalidOperationException: A suitable constructor for type 'Microsoft.Extensions.Logging.LoggerFactory' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.)

    I read through https://docs.unity3d.com/Manual/ManagedCodeStripping.html. My stripping is set to Minimal, I tried the link.xml to no avail and I am looking to try the [Preserve] attribute.

    I am building a 3rd party library that is creating its own logger and don't want to depend on the Unity engine for the [Preserve] attribute. There is a link in the page that is supposed to show you how to create you own attribute that is not dependant on the Unity Engine, but the page is not available. Could someone share the link to that page or any other solution for this code stripping?