Access XAF Application Data in a non-XAF Application

  • 3 minutes to read

In certain scenarios, you may need to create an auxiliary application for database maintenance and use Object Space to query your primary XAF application data. This topic describes how you can create and use Object Space in a regular non-XAF application.

IMPORTANT

We do not recommend using the approach described here in XAF applications. Instead, always use the XafApplication.CreateObjectSpace method or access an existing Object Space.

In a non-XAF application, you have no XafApplication object to create an Object Space. But, an XafApplication does not create Object Spaces itself. Internally, it uses the Object Space Provider designed to create Object Spaces for the currently used ORM and those registered in its overridden XafApplication.CreateDefaultObjectSpaceProvider method. In a non-XAF application, you can instantiate the Object Space Provider manually. Then, you can call the provider's CreateObjectSpace method to create an Object Space.

ORM

Object Space Provider

XPO

XPObjectSpaceProvider / SecuredObjectSpaceProvider

Entity Framework 6

EFObjectSpaceProvider / SecuredObjectSpaceProvider

Entity Framework Core

EFCoreObjectSpaceProvider / SecuredEFCoreObjectSpaceProvider

XPO Example

In XPO-based applications, you should initialize the Types Info Subsystem. The following example demonstrates how to access data of the MainDemo application (typically installed to %PUBLIC%\Documents\DevExpress Demos 20.2\Components.NET Core Desktop Libraries\eXpressApp Framework\MainDemo) and write the list of Departments to the standard output stream:

using DevExpress.ExpressApp.Xpo;
using DevExpress.ExpressApp;
using MainDemo.Module.BusinessObjects;
// ...
class Program {
    static void Main(string[] args) {
        XpoTypesInfoHelper.GetXpoTypeInfoSource();
        XafTypesInfo.Instance.RegisterEntity(typeof(Department));
        XPObjectSpaceProvider osProvider = new XPObjectSpaceProvider(
        @"integrated security=SSPI;pooling=false;data source=(localdb)\v11.0;initial catalog=MainDemo_", null);
        IObjectSpace objectSpace = osProvider.CreateObjectSpace();
        foreach (Department department in objectSpace.GetObjects<Department>()) {
            Console.WriteLine(department.Title + "\t" + department.Office);
        }
    }
}

Entity Framework 6 Example

You can use the following code to access data of the EFDemo application (typically installed to %PUBLIC%\Documents\DevExpress Demos 20.2\Components.NET Core Desktop Libraries\eXpressApp Framework\EFDemoCodeFirst) and write the list of Departments to the standard output stream.

using DevExpress.ExpressApp.EF;
using DevExpress.ExpressApp;
using EFDemo.Module.Data;
// ...
class Program {
    static void Main(string[] args) {
        EFObjectSpaceProvider osProvider = new EFObjectSpaceProvider(typeof(EFDemoDbContext),
            "integrated security=True;multipleactiveresultsets=True;data source=(localdb)\\v11.0;initial catalog=EFDemo_");
        XafTypesInfo.Instance.RegisterEntity(typeof(Department));
        IObjectSpace objectSpace = osProvider.CreateObjectSpace();
        foreach (Department department in objectSpace.GetObjects<Department>()) {
           Console.WriteLine(department.Title + "\t" + department.Office);
        }
    }
}

Entity Framework Core Example

You can use the following code to access data of the EFCore console or WinForms application (demonstrated in this repository) and write the list of Departments to the standard output stream.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.EFCore;
using Microsoft.EntityFrameworkCore;
using System;
// ...
class Program {
    static void Main() {
        EFCoreObjectSpaceProvider objectSpaceProvider = new EFCoreObjectSpaceProvider(typeof(ApplicationDbContext),
            (builder, _) => builder.UseSqlServer(
                "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=EFCoreTestDB;Integrated Security=True;MultipleActiveResultSets=True"));
        XafTypesInfo.Instance.RegisterEntity(typeof(Department));
        IObjectSpace objectSpace = objectSpaceProvider.CreateObjectSpace();
        foreach (Department department in objectSpace.GetObjects<Department>()) {
            Console.WriteLine(department.Title + "\t" + department.Office);
        }
    }
}

Note

If you need to populate the database with initial data, use the DatabaseUpdater.Update method as follows:

using DevExpress.ExpressApp.Updating;
// ...
DatabaseUpdater databaseUpdater = new DatabaseUpdater(
    osProvider, new ModuleBase[0], "", osProvider.ModuleInfoType);
databaseUpdater.Update();
See Also