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. pringBoot-MongoDB 索引冲突分析及解决【华为云技术分享】

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

  2. 链接脚本(Linker Script)用法解析(二) clear_table & copy_table

    可执行文件中的.bss段和.data段分别存放未赋初值的全局变量和已赋初值的全局变量,两者的特点分别为: (1).bss段:①无初值,所以不占ROM空间:②运行时存储于RAM:③默认初值为0 (2). ...

  3. Windows下利用IIS建立网站并实现局域网共享

    https://blog.csdn.net/qq_41485414/article/details/82754252 https://www.cnblogs.com/linuxprobe-sarah/ ...

  4. mybatis错题

    第一题 解析: MyBatis的动态SQL中没有else元素,when元素的test属性中直接书写表达式即可,即test=”表达式”. 第二题 解析: resource属性和url属性是必须的属性,但 ...

  5. 小程序 - 解决IOS端使用css滤镜渲染出现异常

    在页面渲染时,GPU默认不会开启.当css样式中出现某些规则时,就会开启GPU加速,让动画运行的更加流畅,最显著的象征就是元素的3D变换. 这些就是我们通常所说的css硬件加速,但我们有时候并不需要用 ...

  6. Kinect-v2 Examples with MS-SDK(译文二)

    K2-asset提供的脚本组件 K2-asset在KinectScripts文件夹中提供通用脚本组件,并在KinectDemos / 文件夹的相应脚本子文件夹中提供特定于演示的组件.可以在自己的Uni ...

  7. 搞清楚Spring Cloud架构原理的这4个点,轻松应对面试

    前言 现在分布式系统基本上都是标配了,如果你现在还在玩儿单机,没有接触过这些东西的话,权当是为你打开一扇新的大门吧. 大的单体项目 以前我们做单机系统的时候,所有的代码都在一个项目里面,只是不同的模块 ...

  8. PHP ftp获取目录内容为空

    使用PHP的ftp函数获取目录内容,ftp_nlist()和ftp_rawlist()返回都为空. 查了一圈资料找不到答案,然后用Python写了一个,一样的操作就可以获取目录内容. 抓包发现,Pyt ...

  9. 【Java笔试】对数据库中的分解是否为无损连接和是否保持函数依赖的判定-由牛客网试题引申-保姆式教学

    [牛客网数据库原理题目]设关系模式R(A,B,C),F是R上成立的FD集,F={A→B,C→B},ρ={AB,AC}是R的一个分解,那么分解ρ()? 正确答案:C你的答案:A(错误) ( A ) 保持 ...

  10. JS基础知识——原型与原型链

    1.如何准确判断一个变量的数组类型 2.写一个原型链继承的例子 3.描述new一个对象的过程 4.zepto(或其他框架中如何使用原型链) 知识点: (1)构造函数 function Foo(name ...