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

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.

This topic demonstrates how to create a simple XAF application using the Entity Framework (EF) and eXpress Persistent Objects (XPO) business model. For example, if an existing project based XAF XPO reuse of the entity in a non-frame model of XAF application, this method is required. Therefore, your application will access both databases, the first to use XPO, the second uses EF.

Note 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).
  • This topic demonstrates Solution Wizard can automatically generate code. If you want to achieve in the prior XAF feature presentation solutions, please continue. If you want to create a new XAF solutions, please use the wizard. You can select EF and XPO in the "Select ORM" page of the wizard (Entity Framework Code First plus eXpress Persistent Objects).
  • Mobile applications do not support the EF business model.
  • Mobile applications do not support EF business model.
Tip Tip
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E4543
Complete sample project can be found in the code sample database DevExpress, http://www.devexpress.com/example=E4543

Add an EF Data Model in Code

Adding data model 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.
  • Entity and Entity reference frame .dll .SqlServer.dll frame assembly. You can use NuGet automatically download and reference these assemblies (see Getting Entity Framework). Entity Framework support for version 6.
  • Reference the DevExpress.ExpressApp.EF.v19.2.dll assembly, which provides Entity Framework support in XAF.
  • Reference DevExpress.ExpressApp.EF.v19.2.dll assembly, the assembly support frame provision entity in XAF.
  • In the module project, implement the following EntityFrameworkSampleObject and MyDbContext classes (learn more about Entity Framework Code First in XAF).
  • At block project, to achieve the following objects and MyDbContext Entity Framework classes example (first frame for more information about the entity code in the 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

Adding data model code XPO

In the module project, implement the following BaseObject descendant.

In the module project, to achieve the following BaseObject offspring.

 

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

The default object fill space provider 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.

By default, the WinApplication.cs (WinApplication.vb) and WebApplication.cs (WebApplication.vb) file to create an implementation of a default object space provider method to allocate space to the object instance XPObjectSpaceProvider provider parameters. Instead, you can add multiple objects to the object space providers provide space program parameters. XAF will automatically determine what the object space should be used to provide the program to create an object space for each specific business object type. Modify the default Windows Forms and ASP.NET Application project in the following way, "Creating default object space provider" method of implementation.

 

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 the first registered target 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.
  • Obtain Xaf application programs and object space provided Xaf application connection string attribute value.;
  • This transfer from the provider as a compatibility check event Args object space definition provider parameters;
  • To update the application.

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

Ensure that non-persistent object space to provide the first registration program is not an application provider.

Specify Connection Strings for EF and XPO

EF designated connection string 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.

On reading a code from the spatial profile for each object (and the App.config in ASP.NET Web.config Windows Forms application program) providing the connection string program, and thus the connection specified connection StringXpo the connection string StringEF two files.

  • 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

Run the application

Now you can run the application (Windows Forms or ASP.NET) to see that both EF and XPO objects are accessible.

Now, you can run the application (Windows Forms or ASP.NET), and to see the EF XPO objects are accessible.

EFXPO

Tip 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.
If you want to create a program to provide more than one object in registering space objects in code space, please use XafApplication.CreateObjectSpace overloaded method, the method uses the object type parameter. In this case, the object will be created to support a particular type of object space.
Note 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.
When multiple objects in space provider registration application .ObjectSpaceProviders Xaf property, the module updater. After the update the database schema update method will be executed multiple times, once for each execution registered provider. In this method, before accessing specific types of objects, inspection method using IObjectSpace.CanInstantiate current object space supports this type.

Guess you like

Origin www.cnblogs.com/foreachlife/p/How-to-Use-Both-Entity-Framework-and-XPO-in-a-Single-Application.html
Recommended