gpt4 book ai didi

c# - 是否可以从 Python 调用 Entity Framework 数据访问程序集?

转载 作者:行者123 更新时间:2023-11-28 18:29:09 25 4
gpt4 key购买 nike

我正在尝试使用 Robot Framework 来测试 .NET 应用程序。它使用 Python,所以我一直在使用 PythonNet 来调用 .NET 程序集中的方法。现在我遇到了一个问题:调用一个向下调用到基于 Entity Framework 的数据访问程序集的程序集。

我不是直接调用数据访问程序集,而是调用使用它的程序集。但是,我收到一个错误:

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.

我在 C# 中创建了一个类似的测试项目,它在配置文件中不包含 EntityFramework 部分并且它可以工作,所以我知道我不需要配置文件来使用 Entity Framework。

另一个 Stackoverflow 问题,here , 建议有必要引用 EntityFramework.SqlServer.dll。我尝试将以下内容添加到我的 Python 代码中:

clr.AddReference("EntityFramework.SqlServer")

(请注意,.NET bin 文件夹的路径已添加到 Python sys.path 中)

这没有任何区别,我仍然得到同样的错误。

有谁知道如何在 Python 中添加对 EntityFramework.SqlServer.dll 的引用,或者是否绝对不可能从 Python 调用使用 Entity Framework 的 C# 程序集?

编辑:

这是我到目前为止的完整代码:

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)

它在最后一行后失败并出现上述错误。

编辑 2:

抱歉,看来我浪费了你的时间。我尝试将上面的代码粘贴到一个 .py 文件中并运行它,它运行良好。我以前只是以交互方式解决它,并且一直收到上述错误。

至少,如果其他人想知道是否可以从 Python 运行使用 Entity Framework 的 .NET 程序集,那么答案是肯定的。

编辑 3: 结果表明不需要对 EntityFramework 和 EntityFramework.SqlServer 的引用;脚本在没有它们的情况下工作。

最佳答案

在交互式 python 和脚本/模块模式中处理路径的方式略有不同:

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

关于c# - 是否可以从 Python 调用 Entity Framework 数据访问程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38873827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com