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. Java继承、构造、重写

    Music mu=new Music(); Musc m=mu;//地址一样   继承:Java只支持单继承,不支持多继承. Java支持多层(重)继承(继承体系). 如果类之间存在着:is a 的关 ...

  2. su和sudo的区别与使用【华为云技术分享】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...

  3. 【nodejs原理&源码赏析(7)】【译】Node.js中的事件循环,定时器和process.nextTick

    目录 Event Loop 是什么? Event Loop 基本解释 事件循环阶段概览 事件循环细节 timers pending callbacks poll阶段 check close callb ...

  4. JavaEE基础(02):Servlet核心API用法详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.核心API简介 1.Servlet执行流程 Servlet是JavaWeb的三大组件之一(Servlet.Filter.Listener) ...

  5. eclipse没有打断点,项目确仍然要进入断点的问题。

    eclipse没有打断点,却仍然每次debug项目启动时都进入一个断点,很烦人,经在网上查阅发现是勾选了下图中红框中所示的几个选项,把这几项去掉之后就自动不进入断点页面了,经分析这几个选项的大概意思是 ...

  6. Linux之find命令操作技巧

    一.前言 工作中,我们常用find命令查找某些文件或者删除一些旧的日志文件,所以学会find命令的操作技巧就极为重要. 二.find常用参数选项 -depth 从指定目录下最深层的子目录开始查找 -m ...

  7. 关于toString的自动调用

    class a{ } class b extends a{ String rr = "zzz"; public String toString(){ return "aa ...

  8. java8-date和timeAPI

    一 我们为什么要学习 java.timeAPI 原先的Date and Calendar 类的api比较复杂,不易于理解,应用起来不是很灵活. Calendar 是个线程不安全的类会导致SimpleD ...

  9. 《Dotnet9》系列-开源C# WPF控件库强力推荐

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  10. 【Java必修课】图说Stream中的skip()和limit()方法及组合使用

    1 简介 本文将讲解Java 8 Stream中的两个方法:skip()和limit().这两个方法是Stream很常用的,不仅各自会被高频使用,还可以组合出现,并能实现一些小功能,如subList和 ...