Skip to main content
All docs
V21.2
.NET Framework 4.5.2+

DevExpress v24.1 Update — Your Feedback Matters

Our What's New in v24.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Use the Entity Framework 6 Data Model

  • 3 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 21.2\Components\eXpressApp Framework\EFDemoCodeFirst folder.

See Also