[Python] Cannot load an EF Core db context wrapper in Python using Python.NET

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    Crossposting from here

    I have a class called ModelSetWrapper which wraps model sets from an EfCore DbContext (Jet, specifically) and returns a dictionary containing the model set information. Here's what the class looks like:

    namespace LibFacade.Utils;
    public class ModelSetWrapper<T> where T : class
    {
    private Dictionary<string, List<object>> _data;

    public ModelSetWrapper()
    {
    _data = new Dictionary<string, List<object>>();

    PropertyInfo[] properties = typeof(T).GetProperties();

    using (ModelContext context = new ModelContext())
    {
    DbSet<T> dbSet = context.Set<T>();

    foreach (PropertyInfo pInfo in properties)
    {
    List<object> list = dbSet.Select(c => pInfo.GetValue(c)).ToList();

    _data[pInfo.Name] = list;
    }
    }

    }

    public Dictionary<string, List<object>> Data => _data;
    }



    This seems to work fine from the .NET side. From a console app, the following runs without any errors:

    ModelSetWrapper<Jaugeage> wrapper = new ModelSetWrapper<Jaugeage>();
    foreach (KeyValuePair<string, List<object>> kvp in wrapper.Data)
    {
    Console.WriteLine($"{kvp.Key}, {kvp.Value.Count}");
    }


    I'm able to load this assembly and types from it using pythonnet without any issues. However when I attempt the following in my python code:

    wrapper = ModelSetWrapper[Jaugeage]()

    I get an error:

    ---------------------------------------------------------------------------
    InvalidOperationException Traceback (most recent call last)
    Cell In[2], line 1
    ----> 1 wrapper = ModelSetWrapper[Jaugeage]()

    InvalidOperationException: The property can only be set once.
    at EntityFrameworkCore.Jet.Data.JetConnection.set_DataAccessProviderFactory(DbProviderFactory value)
    at EntityFrameworkCore.Jet.Data.JetConnection.Open()
    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
    at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
    at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(Enumerator enumerator)
    at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.<>c.<MoveNext>b__21_0(DbContext _, Enumerator enumerator)
    at EntityFrameworkCore.Jet.Storage.Internal.JetExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
    at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
    at LibFacade.Utils.ModelSetWrapper`1..ctor() in [C:\Users\admin\Desktop\TEMP\projects\317\notebooks\Lib\LibFacade\ModelSetWrapper.cs](file:///C:/Users/admin/Desktop/TEMP/projects/317/notebooks/Lib/LibFacade/ModelSetWrapper.cs):line 12
    at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
    at System.Reflection.MethodBaseInvoker.InvokeConstructorWithoutAlloc(Object obj, Boolean wrapInTargetInvocationException)


    I have searched far an wide for a solution but don't really know how to start tackling this. Any pointers would be very welcome.

    Continue reading...

Compartilhe esta Página