The requested page is not available for the requested platform. You are viewing the content for Default platform.

How to: Use Both Entity Framework and XPO in a Single Application

  • 5 min to read

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.

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).
  • Mobile applications do not support the EF business model.
Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E4543.

Add an EF Data Model in Code

  • 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.
  • Reference the DevExpress.ExpressApp.EF.v19.1.dll assembly, which provides Entity Framework support in XAF.
  • In the module project, implement the following EntityFrameworkSampleObject and MyDbContext classes (learn more about Entity Framework Code First in 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

In the module project, implement the following BaseObject descendant.

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("Name", ref name, value); }
    }
    private string description;
    [Size(SizeAttribute.Unlimited)]
    public String Description { 
        get {return description; }
        set { SetPropertyValue("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.

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:

Ensure that NonPersistentObjectSpaceProvider is not the first registered Provider in your application.

Specify Connection Strings for EF and 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.

<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.

EFXPO

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.

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.

See Also