Use the Entity Framework Data Model

This topic describes how to use the Entity Framework (EF) business model created within the DbContext or ObjectContext entity container in XAF. The business model can be implemented in code (the Code First approach), designed in the Entity Framework Designer (the Model First approach), or generated for an existing database (the Database First approach).

Specify the Entity Container (Context)

The EFObjectSpace object implements the IObjectSpace interface that wraps an entity container. To always access your XAF application's data via EFObjectSpace, modify the default implementations of the CreateDefaultObjectSpaceProvider methods located in the WinApplication.cs (WinApplication.vb) and WebApplication.cs (WebApplication.vb) files.

using DevExpress.ExpressApp.EF;
// ...
protected override void CreateDefaultObjectSpaceProvider(
    CreateCustomObjectSpaceProviderEventArgs args) {
    args.ObjectSpaceProvider = new EFObjectSpaceProvider(typeof(MyDbContext), args.ConnectionString);

In the snippet above, substitute MyDbContext with the type of your entity container. If you use the Code First approach, then the entity container type is the type of your DbContext descendant. If you use Model First, then the container type is specified in the Entity Framework Designer via the Entity Container Name property.

In the Entity Framework, the DbContext or ObjectContext are used to create and manage data. In XPO, there is a Session class that has the same functions. In XAF, an Object Space is used to manipulate data provided by a particular data layer. Generally, Object Space implements an IObjectSpace interface and wraps over the DbContext/ObjectContext if Entity Framework is used, or the Session if XPO is used.

Typically, the DbContext container is used when you create entities in code manually (the Code First workflow), or by using the Entity Framework Designer (the Model First workflow) in Visual Studio 2012/2013. The ObjectContext container is used by default in Visual Studio 2010 when you use the Entity Framework Designer (the Model First workflow) . Additionally, ObjectContext can be used in Visual Studio 2012/2013 if you set the Code Generation Strategy property of your entity model to Legacy ObjectContext.

Specify the Database Connection

In XAF applications, the database connection can be specified via one of the following (see Connect an XAF Application to a Database Provider).

To support a connection string specified in the application configuration file or via the XafApplication.ConnectionString property, the entity container should implement a constructor that takes the connectionString parameter. This constructor will be called by the EFObjectSpaceProvider internally.

public class MyDbContext : DbContext {
    public MyDbContext(string connectionString) : base(connectionString) { }
    // ...

If you use the XafApplication.Connection property instead of the connection string, you do not need this constructor.

Add Entities to the UI

To display your entities in the Navigation System, add the DefaultClassOptionsAttribute or NavigationItemAttribute to their implementations. To hide key properties from the UI, apply Browsable(false) attributes to them. You can apply other built-in attributes as well.

  • Entities implemented within an XAF module are automatically added to the Export to XLS and take part in the UI construction process. If your data model is located in an external assembly which is not an XAF module, refer to the Master-Detail Mode Related Elements topic.
  • Currently, the metadata information on EF classes and their properties is not propagated to the Model Editor at design time. As a result, tools like Filter Builder cannot display object structure. The workaround is provided in the EF - Display data model properties in the design time Model Editor ticket. We will consider supporting this scenario in future XAF versions.

A step-by-step Model First guide is available in the How to: Use the Entity Framework Model First in XAF topic. See How to: Use the Entity Framework Code First in XAF for the Code First tutorial. You can also review the EFDemoCodeFirst and EFDemoModelFirst demo applications that ship with XAF. By default, these demos are installed to the following folders.

  • %PUBLIC%\Documents\DevExpress Demos 18.1\Components\eXpressApp Framework\EFDemoModelFirst
  • %PUBLIC%\Documents\DevExpress Demos 18.1\Components\eXpressApp Framework\EFDemoCodeFirst

See Also