Quantcast
Channel: Developer Express Inc.
Viewing all articles
Browse latest Browse all 2370

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

$
0
0

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]
publicclass EntityFrameworkSampleObject {
[Browsable(false)]
publicint Id { get; protected set; }
publicstring Name { get; set; }
[FieldSize(FieldSizeAttribute.Unlimited)]
public String Description { get; set; }
}

publicclass 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]
publicclass XpoSampleObject : BaseObject {
public XpoSampleObject(Session session) : base(session) { }
privatestring name;
publicstring Name {
get { return name; }
set { SetPropertyValue("Name", ref name, value); }
}
privatestring 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;
// ...
protectedoverridevoid 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>
<addname="ConnectionStringXpo"
connectionString="Integrated Security=SSPI;Pooling=false;Data Source=(local);Initial Catalog=MultipleORMsExampleXpo"/>
<addname="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.


Viewing all articles
Browse latest Browse all 2370

Trending Articles