How to: Use Both Entity Framework and XPO in a Single Application 如何:在单个应用程序中同时使用实体框架和 XPO

时间:2021-09-11 14:22:08

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 对象均可访问。

How to: Use Both Entity Framework and XPO in a Single Application 如何:在单个应用程序中同时使用实体框架和 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 方法检查当前对象空间是否支持此类型。