Skip to main content
A newer version of this page is available.

Use the Entity Framework 6 Data Model

  • 4 minutes to read

This topic describes how to use the Entity Framework 6 (EF 6) 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 6, 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 6 is used, or the Session if XPO is used.

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.

Note

  • 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.

See How to: Use the Entity Framework Code First in XAF for the Code First tutorial. You can also review the EFDemoCodeFirst demo application that ship with XAF. By default, this demo is installed to the %PUBLIC%\Documents\DevExpress Demos 20.2\Components.NET Core Desktop Libraries\eXpressApp Framework\EFDemoCodeFirst folder.

See Also