3

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.

Community
  • 1
  • 1
Simon Elms
  • 17,832
  • 21
  • 87
  • 103

1 Answers1

1

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

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

denfromufa
  • 5,610
  • 13
  • 81
  • 138