How to: Use Both Entity Framework 6 and XPO in a Single Application
- 5 minutes to read
This topic demonstrates how to create a simple XAF application that uses both the Entity Framework 6 (EF 6) and eXpress Persistent Objects (XPO) business models. For instance, this approach is required if you want to reuse the Entity Framework 6 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 6.
Note
The Solution Wizard generates the code shown in this help topic when you create an application. Follow this article if you want to implement the demonstrated functionality in an existing XAF solution. You can choose both EF 6 and XPO in the Choose ORM page of the wizard (Entity Framework 6 plus e
In ASP.
Tip
A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.
#Add an EF 6 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.EF6.v21.2.dll assembly, which provides Entity Framework 6 support in XAF.
In the module project, implement the following EntityFrameworkSampleObject and MyDbContext classes (learn more about Entity Framework 6 Code First in XAF).
using System.ComponentModel; using EntityFramework; 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(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 Web Forms 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:
- to get XafApplication.ObjectSpaceProvider and XafApplication.ConnectionString property values;
- to pass this Provider as the CustomCheckCompatibilityEventArgs‘s ObjectSpaceProvider argument;
- to update an application.
Ensure that NonPersistentObjectSpaceProvider is not the first registered Provider in your application.
#Specify Connection Strings for EF 6 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 Web Forms), 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 Web Forms) to see that both EF 6 and XPO objects are accessible.
Tip
If you want to create an Object Space in code when several Object Space providers are registered, use the overload of Xaf
Note
When multiple Object Space Providers are registered in the Xaf