Blogs

eXpress App Framework Team

XAF Core Improvements – The Entity Framework and XPO Data Models in One Application (Coming in 12.2.7)

     

XAF team is relentlessly working to improve Entity Framework (EF) support. In the previous minor update, we have extended the list of EF-compatible extra modules with the Chart, Pivot Grid and Tree List modules, so the most of popular modules are compatible now. In the upcoming 12.2.7 update, we introduce an option to use both the Entity Framework and XPO business models in one application. For instance, with this feature you can reuse the EF 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 via XPO and the second - via EF. Let us see the example.

Add an EF Data Model in Code

In the module project, reference the System.Data.Entity.dll, EntityFramework.dll and DevExpress.ExpressApp.EF.v12.2.dll assemblies and implement the following EntityFrameworkSampleObject and MyDbContext classes.

using System.ComponentModel;

using System.Data.Entity;
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; }
}

To enable the automatic collection of EF Code First entities, add the following code to the module's constructor located in the Module.cs file.

using DevExpress.ExpressApp.EF;
// ...
public MySolutionModule() {
// ...
ExportedTypeHelpers.AddExportedTypeHelper(new EFExportedTypeHelperCF());
}

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 and WebApplication.cs 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 EFObjectSpaceProviderCF(
typeof(MyDbContext), (TypesInfo)TypesInfo, null,
ConfigurationManager.ConnectionStrings["ConnectionStringEF"].ConnectionString));
}


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;Pooling=false;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.

EF XPO_Office2013

 

The EntityFrameworkSampleObject objects are persisted to the MultipleORMsExampleEF database, and XpoSampleObject – to MultipleORMsExampleXpo.

EF XPO_DB

Hope this small feature will be helpful when you set yourself a task like Q433494.

Published Feb 27 2013, 01:40 AM by Konstantin B (DevExpress)
Bookmark and Share

Comments

 

Steven Rasmussen said:

I'm assuming that you could also set this up so that you can access 2 (or more) XPO object space providers so that you could connect to multiple databases?  Is that correct?

February 27, 2013 7:28 PM
 

Konstantin B (DevExpress) said:

Steven, you are correct. I'm going to publish an example on how to use several XPO databases simultaneously. BTW, with EF this task is very simple - just declare several Contexts and register an Object Space Provider with unique connection string for each Context. With XPO, you need to explictly specify a database for  each persistent class, as there is no Context analog in XPO.

February 28, 2013 2:01 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.