Entity Framework and ironpython code first

Is it possible to use Entity Framework 6 with the first code template and write models in C # and in IronPython?

The background is that several standard models are defined in the C # core, and some specific models must be defined in IronPython.

EDIT

Custom models have been defined in our productive systems for customization. Models must be loaded and added to the DbContext when the application starts. Each model is a single IronPython file / module and will be loaded from the database (like all other scripts).

Sample not working

Program.cs

 using IronPython.Hosting; using Microsoft.Scripting.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IronPython_EF { class Program { // Private Member private static SampleContext context; static void Main(string[] args) { // Setup EntityFramework context = new SampleContext(); // Create IronPython var setup = Python.CreateRuntimeSetup(null); var runtime = new ScriptRuntime(setup); var engine = runtime.GetEngine("IronPython"); var scriptScope = engine.CreateScope(); // Set global var scriptScope.SetVariable("DBContext", context); // Load Model ScriptSource modelSource = engine.CreateScriptSourceFromFile("Python\\User.py"); modelSource.Execute(scriptScope); ScriptSource scriptSource = engine.CreateScriptSourceFromFile("Python\\Demo.py"); scriptSource.Execute(scriptScope); // Prevent from exiting Console.ReadLine(); } } } 

SampleContext.cs

 namespace IronPython_EF { using System; using System.Data.Entity; using System.Linq; public class SampleContext : DbContext { public SampleContext() : base("name=SampleContext") { } } } 

User.Py

 # ----------------------------------------------- # Containing the UserModel # ----------------------------------------------- # ----------------------------------------------- # Import from System import Console # ----------------------------------------------- # class User(object): userId = 0 userName = "" def __init__(self): pass def get_userId(self): return self.userId; def set_userId(self, value): self.userId = value def get_userName(self): return self.userName; def set_userId(self, value): self.userName = value UserId = property(get_userId, set_userId) UserName = property(get_userName, set_userId) 

Demo.py

 # ----------------------------------------------- # Demo Python Script, executing something in the # Sample-DB-Context # ----------------------------------------------- # ----------------------------------------------- # Import from System import Console # ----------------------------------------------- # Console.WriteLine("Executing demo.py") # Add User-Model userSet = DBContext.Set[User]() # Add User instance usr = User() usr.UserName = "Hallo Welt" userSet.Add(usr) # Save changes DBContext.SaveChanges() 

I get an exception that User not part of DBContext.

Thank you for your help!

+5
source share
1 answer

I'm not sure if this is possible since IronPython classes are not .NET classes. It may be possible to write your own resolver than generate classes for you from IronPython files. But this can be complicated by relationships / indexes / keys, etc.

If you want to keep IronPython, and if you are looking for the benefits of EF for EF, then the alternative would be: http://www.sqlalchemy.org/

But if you are looking for another kindness that EF provides (migration, etc.) then this will be a bit of a problem.

0
source

Source: https://habr.com/ru/post/1214254/


All Articles