Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call an Entity Framework Data Access Assembly from Python?

I'm experimenting with using Robot Framework for testing .NET applications. It uses Python so I've been using PythonNet to call methods in the .NET assemblies. Now I've struck a problem: Calling an assembly that calls down into an Entity Framework-based data access assembly.

I'm not calling the data access assembly directly, but the assembly that uses it. However, I'm getting an error:

System.InvalidOperationException: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file.

I've created a similar test project in C# which does not include an EntityFramework section in the config file and it works, so I know I don't need a config file to use Entity Framework.

Another Stackoverflow question, here, suggested that it was necessary to reference the EntityFramework.SqlServer.dll. I tried adding the following to my Python code:

clr.AddReference("EntityFramework.SqlServer")

(note that the path to the .NET bin folder has been added to the Python sys.path already)

This didn't make any difference, I still got the same error.

Does anyone know how to add a reference to EntityFramework.SqlServer.dll in Python or, alternatively, whether it's definitely not possible to call a C# assembly that uses Entity Framework from Python?

EDIT:

Here's the full code that I have so far:

import clr
import sys
sys.path.append(r"C:\Users\...\bin\Debug")

clr.AddReference("SchoolGrades")
clr.AddReference("DataAccess")
clr.AddReference("Models")

clr.AddReference("EntityFramework")
clr.AddReference("EntityFramework.SqlServer")

from SchoolGrades import SchoolRoll
from DataAccess import SchoolContext
from Models import Student

connectionString = r"Data Source=(LocalDb)\mssqllocaldb;Initial Catalog=DataAccess.SchoolContext;Integrated Security=True"
context = SchoolContext(connectionString)
school = SchoolRoll(context)

student = Student()
student.StudentName = "Python Student"
school.AddStudent(student)

It fails with the error above after the last line.

EDIT 2:

Sorry, looks like I've wasted your time. I tried pasting the code above into a .py file and running it, and it worked perfectly. I had previously just been working it out interactively and had been getting the error above.

At least if anyone else wants to know if it's possible to run a .NET assembly that uses Entity Framework from Python, the answer is yes.

EDIT 3: Turns out the references to EntityFramework and EntityFramework.SqlServer are not needed; the script works without them.

like image 812
Simon Tewsi Avatar asked Mar 08 '26 13:03

Simon Tewsi


1 Answers

The way the paths are treated in interactive python and script/module mode are slightly different:

https://github.com/pythonnet/pythonnet/issues/98

like image 134
denfromufa Avatar answered Mar 11 '26 01:03

denfromufa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!