在运行时修改实体框架模型 [英] Modifying an Entity Framework Model at Run-Time

查看:27
本文介绍了在运行时修改实体框架模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这纯粹是与 EF4 相关的概念和设计理念.

This is purely a conceptual and design idea related to EF4.

示例/场景是一个大型 ERP 或 CRM 类型的系统,公司可能需要在其中添加传统的用户定义字段"来捕获更多数据.

The example/scenario is a large ERP or CRM type system where companies may need to add traditional "user defined fields" to capture additional data.

我知道 EF 有一些功能可以在运行时将模型推送到数据库,但问题是您能否使用 EF 实时修改模型并更新数据存储?

I know that the EF has some capabilities to push out the model to a database at run-time but the question really is can you use EF to modify a model and update the data store in real-time?

换句话说,如果我为用户提供一种机制来添加用户定义的列、关联的数据类型和空要求,是否可以使用 EF 即时完成,然后在所有未来会话中记住?

In other words, if I provide the user a mechanism to add a user defined column, associated data type and null requirements can that be done on the fly with EF and then remembered for all future sessions?

它就在那里,但我想你们都会看到我在说什么.

It's out there but I think you all will see what I'm getting at.

布伦特

推荐答案

过去我被问过几次这个问题.没有明显或简单的方法.可能没有办法,但我们是开发人员,总有办法!我知道那是什么吗?不.我能想出一些主意吗?....嗯.. 在运行时,该模型基于来自元数据工作区的强类型类.您可以即时创建它们.但是随后您需要修改 edmx 文件的 xml.为此可以使用 LINQ to XML 或 xpath.修改数据库架构...模型首先如何构建dbs...它创建sql,然后您执行它.您必须创建 sql(如何?耸肩)并执行它(objectcontext.executestorecommand()).可行的?可能的?我没有任何线索.真的答案是否定的……据我所知,VS 或 .NET 4(EF API)中没有任何内容可以轻松启用此功能.肯定有人比我更聪明、更有耐心(浪费了??)很多时间(试图)智胜 EF 以实现这一目标.但是,根据您对 Jeremy 博客文章的建议的回应,您正在寻找内置/方便的东西.用不"来回答更容易.

I've been asked this question a few times in the past. There is no obvious or easy way. It's possible that there is no way but we are developers and there's always a way! Do I know what that is? No. Can I dream up some ideas? ....Mmmm.. at runtime, the model is based on strongly typed classes from the metadataworkspace. You can create those on the fly. But then you need to modify the xml of the edmx file.There's LINQ to XML or xpath for that. Modifying the database schema... how does model first build dbs...it creates sql and then you execute it. You'd have to create sql (how? shrug) and the execute it (objectcontext.executestorecommand()). Feasible? Possible? I have no clue. Really the answer is no...there's nothing in VS or .NET 4 (EF APIs) to readily enable this as far as I know. Surely someone more clever and patient than I was spent (wasted??) a lot of time (trying to) outsmart EF to pull this off. Based, however, on your response t the suggestion re Jeremy's blog post, you're looking for something built in/handy. That is easier to answer with a "nope".

朱莉

这篇关于在运行时修改实体框架模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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