实体框架代码优先和IronPython [英] Entity Framework code first and ironpython

查看:109
本文介绍了实体框架代码优先和IronPython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Entity Framework 6与代码优先模式一起使用,并用C#和IronPython编写模型?

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

背景是,在c#内核中定义了一些标准模型,而在IronPython中必须定义一些自定义模型.

The background is, that a few standard models are defined in the c# core, and some custom models has to be defined in IronPython.

编辑

在我们的生产系统中定义了定制模型以进行定制.应在应用程序启动时将模型加载并添加到DbContext中.每个模型都是一个IronPython文件/模块,并将从数据库中加载(就像所有其他脚本一样).

The custom models were defined in our productive systems for customization. The models should be loaded and added to the DbContext at application startup. Every model is one IronPython file/module and will be loaded from a database (as all other scripts will be).

无法正常工作的示例

Program.cs

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

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()

我得到一个例外,那就是User不是DBContext的一部分.

I get the exception, that User is not part of the DBContext.

谢谢您的帮助!

推荐答案

我不确定这是可能的,因为IronPython类不是.NET类. 可能会编写一个自定义映射器,而不是从IronPython文件为您生成类.但这可能因关系/索引/键等而变得复杂.

I'm not sure this is possible as IronPython classes are not .NET classes. It might be possible to write a custom mapper than will generate the classes for you from the IronPython files. But this could get complicated with relationships/indexes/keys etc.

如果您必须保留IronPython,并且正在寻找EF的ORM好处,那么可以选择以下方法: http://www.sqlalchemy.org/

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

但是,如果您正在寻找EF提供的其他优点(迁移等),那么这将是一个问题.

But if you are looking for the other goodness that EF provides (migrations etc), then it is going to be a bit of a problem.

这篇关于实体框架代码优先和IronPython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆