How to: Use Both Entity Framework and XPO in a Single Application 如何:在单个应用程序中同时使用实体框架和 XPO
This topic demonstrates how to create a simple XAF application that uses both the Entity Framework (EF) and eXpress Persistent Objects (XPO) business models. For instance, this approach is required if you want to reuse the Entity Framework model from a non-XAF application in your existing XPO-based XAF project. As a result, your application will access two databases, the first one using XPO and the second using EF.
本主题演示如何创建使用实体框架 (EF) 和 eXpress 持久对象 (XPO) 业务模型的简单 XAF 应用程序。例如,如果要在现有基于 XPO 的 XAF 项目中重用非 XAF 应用程序中的实体框架模型,则需要此方法。因此,您的应用程序将访问两个数据库,第一个使用 XPO,第二个使用 EF。
Note 注意
This topic demonstrates the code that can be generated automatically by the Solution Wizard. Proceed, if you want to implement the demonstrated functionality in the existing XAF solution. If you are creating a new XAF solution, use the wizard instead. You can choose both EF and XPO in the Choose ORM page of the wizard (Entity Framework Code First plus eXpress Persistent Objects).
本主题演示解决方案向导可以自动生成的代码。如果要在现有 XAF 解决方案中实现演示的功能,请继续。如果要创建新的 XAF 解决方案,请使用向导。您可以在向导的"选择 ORM"页中选择 EF 和 XPO(实体框架代码优先加上 eXpress 持久对象)。
Mobile applications do not support the EF business model.
移动应用程序不支持 EF 业务模式。
Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E4543
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E4543
Add an EF Data Model in Code
在代码中添加 EF 数据模型
- Reference the EntityFramework.dll and EntityFramework.SqlServer.dll assemblies. You can use NuGet to download and reference these assemblies automatically (see Get Entity Framework). The supported Entity Framework version is 6.
- 引用实体框架.dll 和实体框架.SqlServer.dll 程序集。您可以使用 NuGet 自动下载和引用这些程序集(请参阅获取实体框架)。支持的实体框架版本为 6。
- Reference the DevExpress.ExpressApp.EF.v19.2.dll assembly, which provides Entity Framework support in XAF.
- 参考 DevExpress.ExpressApp.EF.v19.2.dll 程序集,该程序集在 XAF 中提供实体框架支持。
- In the module project, implement the following EntityFrameworkSampleObject and MyDbContext classes (learn more about Entity Framework Code First in XAF).
- 在模块项目中,实现以下实体框架示例对象和 MyDbContext 类(首先在 XAF 中了解有关实体框架代码的更多信息)。
using System.ComponentModel;
using EntityFramework.dll;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.DC;
// ...
[DefaultClassOptions]
public class EntityFrameworkSampleObject {
[Browsable(false)]
public int Id { get; protected set; }
public string Name { get; set; }
[FieldSize(FieldSizeAttribute.Unlimited)]
public String Description { get; set; }
}
public class MyDbContext : DbContext {
public MyDbContext(string connectionString) : base(connectionString) { }
public DbSet<EntityFrameworkSampleObject> SampleObjects { get; set; }
}
Add an XPO Data Model in Code
在代码中添加 XPO 数据模型
In the module project, implement the following BaseObject descendant.
在模块项目中,实现以下 BaseObject 后代。
using DevExpress.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.DC;
// ...
[DefaultClassOptions]
public class XpoSampleObject : BaseObject {
public XpoSampleObject(Session session) : base(session) { }
private string name;
public string Name {
get { return name; }
set { SetPropertyValue(nameof(Name), ref name, value); }
}
private string description;
[Size(SizeAttribute.Unlimited)]
public String Description {
get {return description; }
set { SetPropertyValue(nameof(Description), ref description, value); }
}
}
Populate the DefaultObjectSpaceProviders Collection
填充默认对象空间提供程序集合
By default, the CreateDefaultObjectSpaceProvider method implemented in the WinApplication.cs (WinApplication.vb) and WebApplication.cs (WebApplication.vb) files assigns an XPObjectSpaceProvider instance to the ObjectSpaceProvider parameter. Instead, you can add multiple Object Space Providers to the ObjectSpaceProviders parameter. XAF will automatically determine what Object Space Provider should be used to create an Object Space for each particular business object type. Modify the default implementation of the CreateDefaultObjectSpaceProvider method for both Windows Forms and ASP.NET application projects in the following manner.
默认情况下,在WinApplication.cs (WinApplication.vb) 和WebApplication.cs (WebApplication.vb) 文件中实现的创建默认对象空间提供程序方法将 XPObjectSpaceProvider 实例分配给对象空间提供商参数。相反,您可以将多个对象空间提供程序添加到对象空间提供程序参数。XAF 将自动确定应该使用什么对象空间提供程序为每个特定的业务对象类型创建对象空间。以下列方式修改 Windows 窗体和ASP.NET应用程序项目的"创建默认对象空间提供程序"方法的默认实现。
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.EF;
// ...
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
args.ObjectSpaceProviders.Add(
new XPObjectSpaceProvider(ConfigurationManager.ConnectionStrings["ConnectionStringXpo"].ConnectionString, null));
args.ObjectSpaceProviders.Add(
new EFObjectSpaceProvider(typeof(MyDbContext),
ConfigurationManager.ConnectionStrings["ConnectionStringEF"].ConnectionString));
}
XAF uses the first registered Object Space Provider for the following purposes:
XAF 将第一个已注册的对象空间提供程序用于以下目的:
- to get XafApplication.ObjectSpaceProvider and XafApplication.ConnectionString property values;
- to pass this Provider as the CustomCheckCompatibilityEventArgs's ObjectSpaceProvider argument;
- to update an application.
- 获取 Xaf 应用程序.对象空间提供程序和 Xaf 应用程序.连接字符串属性值;
- 传递此提供程序作为自定义检查兼容性事件Args的对象空间提供者参数;
- 以更新应用程序。
Ensure that NonPersistentObjectSpaceProvider is not the first registered Provider in your application.
确保非持久对象空间提供程序不是应用程序中的第一个注册提供程序。
Specify Connection Strings for EF and XPO
为 EF 和 XPO 指定连接字符串
The code in the previous section reads connection strings for each Object Space Provider from the configuration file (App.config in a Windows Forms application project and Web.config in ASP.NET), so specify the ConnectionStringXpo and ConnectionStringEF connection strings in both files.
上一节中的代码从配置文件(Windows 窗体应用程序项目中的 App.config 和ASP.NET中的 Web.config)读取每个对象空间提供程序的连接字符串,因此指定连接StringXpo 和连接StringEF两个文件中的连接字符串。
- XML
<connectionStrings>
<add name="ConnectionStringXpo" connectionString="Integrated Security=SSPI;
Pooling=false;Data Source=(local);Initial Catalog=MultipleORMsExampleXpo" />
<add name="ConnectionStringEF" connectionString="Integrated Security=SSPI;
Data Source=(local);Initial Catalog=MultipleORMsExampleEF" />
</connectionStrings>
Run the Application
运行应用程序
Now you can run the application (Windows Forms or ASP.NET) to see that both EF and XPO objects are accessible.
现在,您可以运行应用程序(Windows 窗体或ASP.NET),以查看 EF 和 XPO 对象均可访问。

Tip 提示
If you want to create an Object Space in code when several Object Space providers are registered, use the overload of XafApplication.CreateObjectSpace method that takes the objectType parameter. In this instance, an Object Space that supports a particular object type will be created.
如果要在注册多个对象空间提供程序时在代码中创建对象空间,请使用 XafApplication.CreateObjectSpace 方法的重载,该方法采用对象类型参数。在这种情况下,将创建支持特定对象类型的对象空间。
Note 注意
When multiple Object Space Providers are registered in the XafApplication.ObjectSpaceProviders property, the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method is executed multiple times, once for each registered Provider. In this method, before accessing an object of a particular type, check if the current Object Space supports this type using the IObjectSpace.CanInstantiate method.
当多个对象空间提供程序注册在 Xaf 应用程序.ObjectSpaceProviders 属性中时,模块更新器.更新数据库后更新架构方法将执行多次,每个已注册的提供程序执行一次。在此方法中,在访问特定类型的对象之前,使用 IObjectSpace.CanInstantiate 方法检查当前对象空间是否支持此类型。
How to: Use Both Entity Framework and XPO in a Single Application 如何:在单个应用程序中同时使用实体框架和 XPO的更多相关文章
- 《Entity Framework 6 Recipes》中文翻译系列 (20) -----第四章 ASP.NET MVC中使用实体框架之在MVC中构建一个CRUD示例
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第四章 ASP.NET MVC中使用实体框架 ASP.NET是一个免费的Web框架 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (21) -----第四章 ASP.NET MVC中使用实体框架之在页面中创建查询和使用ASP.NET URL路由过虑
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 4.2. 构建一个搜索查询 搜索数据是几乎所有应用的一个基本功能.它一般是动态的,因 ...
- entity framework codefirst 用户代码未处理DataException,InnerException基础提供程序在open上失败,数据库生成失败
警告:这是一个入门级日志,如果你很了解CodeFirst,那请绕道 背景:这篇日志记录我使用Entity FrameWork CodeFirst时出现的错误和解决问题的过程,虽然有点曲折……勿喷 备注 ...
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-fi ...
- Entity Framework 6 Recipes 2nd Edition(9-7)译->在WCF服务中序列化代理
9-7. 在WCF服务中序列化代理 问题 从一个查询里返回一个动态代理对象,想要把它序列为一个POCO(Plain-Old CLR Objects)对象. 实现基于POCO实体对象, 在运行时,EF会 ...
- Entity Framework 6 Recipes 2nd Edition(10-6)译 -> TPT继承模型中使用存储过程
10-6. TPT继承模型中使用存储过程 问题 想在一个TPT继承模型中使用存储过程 解决方案 假设已有如Figure 10-6所示模型. 在模型里, Magazine(杂志) and DVD继承于基 ...
- Entity Framework 6 Recipes 2nd Edition(10-7)译 -> TPH继承模型中使用存储过程
10-7. TPH继承模型中使用存储过程 问题 用一个存储过程来填充TPH继承模型的实体 解决方案 假设已有如Figure 10-7所示模型. 我们有两个派生实体: Instructor(教员)和St ...
- Entity Framework 6 Recipes 2nd Edition(13-7)译 -> 返回只部分填充的实体
问题 你有一个实体里的某个属性很少被读取或和更新,这个属性因为比较大,所以读取和更新都需要付很大的代价.你想有选择的放置这个属性 解决方案 假设你有一个如Figure 13-9 所示的模型 Figur ...
- Entity Framework 6 Recipes 2nd Edition(10-5)译 -> 在存储模型中使用自定义函数
10-5. 在存储模型中使用自定义函数 问题 想在模型中使用自定义函数,而不是存储过程. 解决方案 假设我们数据库里有成员(members)和他们已经发送的信息(messages) 关系数据表,如Fi ...
随机推荐
- java前端与后端怎么选??
想做这个行业,就应该了解职能以及技能需求,这样学习才能更高效.我知道一些刚刚入行的小伙伴不清楚前端.后端.到底指的是什么?两者直接的区别 前端开发 前端开发主要涉及网站和App,用户能够从浏览器上或A ...
- 转:spring aop 拦截业务方法,实现权限控制
难点:aop类是普通的java类,session是无法注入的,那么在有状态的系统中如何获取用户相关信息呢,session是必经之路啊,获取session就变的很重要.思索很久没有办法,后来在网上看到了 ...
- 2019 ICPC上海网络赛 A 题 Lightning Routing I (动态维护树的直径)
题目: 给定一棵树, 带边权. 现在有2种操作: 1.修改第i条边的权值. 2.询问u到其他一个任意点的最大距离是多少. 题解: 树的直径可以通过两次 dfs() 的方法求得.换句话说,到任意点最远的 ...
- HDU2767 Proving Equivalences(加边变为强联通图)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HihoCoder1449 后缀自动机三·重复旋律6
描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数列. 现在小Hi想知道一部作品中所有长度为K的旋律中出现次数最多的旋律的出现次数.但是K不是固定的,小Hi想知道对 ...
- BZOJ [HNOI2006]鬼谷子的钱袋
1192: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5367 Solved: 3646[Submit][St ...
- Zabbix 监控MySQL、Apache、Nginx应用监控
zabbix对第三方应用软件的监控,主要有两个工作难点,一个是编写自定义监控脚本,另一个是在编写模板并导入zabbix web中,编写脚本这个要根据 监控需求定制即可,而编写模板文件有些难度,不过网上 ...
- zabbix系列-Grafana4.6.3+Zabbix 的安装部署
zabbix系列(五) Grafana4.6.3+Zabbix 的安装部署 伟创享 2019-07-31 11:27:18 使用了一段时间Grafana,感觉还挺好用的.部分效果图如下: zabb ...
- 《java面试十八式》--引子
爪哇城中 “喂,你等等我啊”少女气喘吁吁的喊道 “大小姐,你可快点吧,报名马上就要结束了.” 这是爪哇城一年一度的大选比赛,被选上的人会留下来任职,享有名誉和金钱,所以大家都在积极准备. ...
- 手把手教你打造一款轻量级canvas渲染引擎
背景 当我们开发一个canvas应用的时候,出于效率的考量,免不了要选择一个渲染引擎(比如PixiJS)或者更强大一点的游戏引擎(比如Cocos Creator.Layabox). 渲染引擎通常会有S ...