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的更多相关文章

  1. 《Entity Framework 6 Recipes》中文翻译系列 (20) -----第四章 ASP.NET MVC中使用实体框架之在MVC中构建一个CRUD示例

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第四章  ASP.NET MVC中使用实体框架 ASP.NET是一个免费的Web框架 ...

  2. 《Entity Framework 6 Recipes》中文翻译系列 (21) -----第四章 ASP.NET MVC中使用实体框架之在页面中创建查询和使用ASP.NET URL路由过虑

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 4.2. 构建一个搜索查询 搜索数据是几乎所有应用的一个基本功能.它一般是动态的,因 ...

  3. entity framework codefirst 用户代码未处理DataException,InnerException基础提供程序在open上失败,数据库生成失败

    警告:这是一个入门级日志,如果你很了解CodeFirst,那请绕道 背景:这篇日志记录我使用Entity FrameWork CodeFirst时出现的错误和解决问题的过程,虽然有点曲折……勿喷 备注 ...

  4. [转]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 ...

  5. Entity Framework 6 Recipes 2nd Edition(9-7)译->在WCF服务中序列化代理

    9-7. 在WCF服务中序列化代理 问题 从一个查询里返回一个动态代理对象,想要把它序列为一个POCO(Plain-Old CLR Objects)对象. 实现基于POCO实体对象, 在运行时,EF会 ...

  6. Entity Framework 6 Recipes 2nd Edition(10-6)译 -> TPT继承模型中使用存储过程

    10-6. TPT继承模型中使用存储过程 问题 想在一个TPT继承模型中使用存储过程 解决方案 假设已有如Figure 10-6所示模型. 在模型里, Magazine(杂志) and DVD继承于基 ...

  7. Entity Framework 6 Recipes 2nd Edition(10-7)译 -> TPH继承模型中使用存储过程

    10-7. TPH继承模型中使用存储过程 问题 用一个存储过程来填充TPH继承模型的实体 解决方案 假设已有如Figure 10-7所示模型. 我们有两个派生实体: Instructor(教员)和St ...

  8. Entity Framework 6 Recipes 2nd Edition(13-7)译 -> 返回只部分填充的实体

    问题 你有一个实体里的某个属性很少被读取或和更新,这个属性因为比较大,所以读取和更新都需要付很大的代价.你想有选择的放置这个属性 解决方案 假设你有一个如Figure 13-9 所示的模型 Figur ...

  9. Entity Framework 6 Recipes 2nd Edition(10-5)译 -> 在存储模型中使用自定义函数

    10-5. 在存储模型中使用自定义函数 问题 想在模型中使用自定义函数,而不是存储过程. 解决方案 假设我们数据库里有成员(members)和他们已经发送的信息(messages) 关系数据表,如Fi ...

随机推荐

  1. Lua的面向对象,封装,继承,多态

    概述 我们总所周知对象是由属性和方法组成的,要用lua要描述一个对象,也必然要有这两个特性,属性和方法.lua的基本结构是table,所以Lua的类,其实都是table,因为它可以存储普通的变量又可以 ...

  2. 使用app测试Modelarts在线服务

    1. 基础准备 本demo代码已上传github地址为 https://github.com/zxzxzxygithub/hwmodelartdemo clone下来之后导入android studi ...

  3. [TimLinux] asciinema Linux终端录制工具嵌入私有web中

    yum install asciinema https://github.com/asciinema/asciinema-player # 下载asciinema-player.css, asciin ...

  4. ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  5. 深入学习 OLED Adafruit_SSD1306库(8266+arduino)

    QQ技术互动交流群:ESP8266&32 物联网开发 群号622368884,不喜勿喷 单片机菜鸟博哥CSDN 1.前言 SSD1306屏幕驱动库,最出名应该就是u8g2,读者可以参考 玩转u ...

  6. BOM和DOM操作

    目录 BOM window对象 window子对象 location 弹出框 计时 history navigator DOM 查找节点 直接查找 间接查找 节点操作 创建节点 添加节点 删除节点 替 ...

  7. 【系列专题】JavaScript 重温系列(22篇全)

    JavaScript 初级篇 [JS]120-重温基础:语法和数据类型 [JS]121-重温基础:流程控制和错误处理 [JS]122-重温基础:循环和迭代 [JS]123-重温基础:函数 [JS]12 ...

  8. 【搞定Jvm面试】 JVM 垃圾回收揭秘附常见面试题解析

    JVM 垃圾回收 写在前面 本节常见面试题 问题答案在文中都有提到 如何判断对象是否死亡(两种方法). 简单的介绍一下强引用.软引用.弱引用.虚引用(虚引用与软引用和弱引用的区别.使用软引用能带来的好 ...

  9. 前端js分页功能的实现,非常实用,新手必看

    分享一个前端拿到数据后,自己生成分页的案例,案例如图显示,点击搜索后查询数据,显示数据列表,前端根据数据显示正确的分页 附上代码:代码附上,简单粗暴,自己用过的,兼容性好,没有封装,可以很好的分页原理 ...

  10. Java读取Properties文件 Java加载配置Properties文件

    static{ Properties prop = new Properties(); prop.load(Thread.currentThread().getContextClassLoader() ...