Skip to main content
All docs
.NET Framework 4.5.2+

Use the Entity Framework Core Data Model

  • 3 minutes to read

This topic describes how to use the Entity Framework Core (EF Core) business model created within the DbContext entity container in XAF.

Read Tutorial: Define the Data Model and Set the Initial Data with EF Core

Specify the Entity Container (Context)

In Entity Framework Core, use DbContext to create and manage data.

File: YourApplicationName.Module\BusinessObjects\YourApplicationNameDbContext.cs

// ...
using DevExpress.ExpressApp.Design;
using DevExpress.ExpressApp.EFCore.DesignTime;

namespace YourApplicationName.Module.BusinessObjects;

public class YourApplicationNameContextInitializer : DbContextTypesInfoInitializerBase {
    protected override DbContext CreateDbContext() {
        var optionsBuilder = new DbContextOptionsBuilder<YourApplicationNameEFCoreDbContext>()
        return new YourApplicationNameEFCoreDbContext(optionsBuilder.Options);

public class YourApplicationNameDesignTimeDbContextFactory : IDesignTimeDbContextFactory<YourApplicationNameEFCoreDbContext> {
    public YourApplicationNameEFCoreDbContext CreateDbContext(string[] args) {
        throw new InvalidOperationException("Make sure that the database connection string and connection provider are correct. After that, uncomment the code below and remove this exception.");
public class YourApplicationNameEFCoreDbContext : DbContext {
    public YourApplicationNameEFCoreDbContext(DbContextOptions<YourApplicationNameEFCoreDbContext> options) : base(options) { }
    //public DbSet<ModuleInfo> ModulesInfo { get; set; }
    public DbSet<ModelDifference> ModelDifferences { get; set; }
    public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; }
    // ...

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        // ...

Specify the Database Connection


XAF requires Multiple Active Result Sets in EF Core-based applications connected to a Microsoft SQL Server database. We do not recommend that you remove MultipleActiveResultSets=True; from the connection string or set the MultipleActiveResultSets parameter to false. For other database management systems, we implement a mechanism internally that allows an application to correctly run without Multiple Active Result Sets.

ASP.NET Core Blazor

In ASP.NET Core Blazor applications, you can specify the database connection in the ConnectionString section in the configuration file (MySolution.Blazor.Server\appsettings.json):

  "ConnectionStrings": {
    "ConnectionString": "Integrated Security=SSPI;Pooling=false;MultipleActiveResultSets=True;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=MySolution",
    // ...
  // ...

Windows Forms

In Windows Forms applications, you can specify the database connection. For additional information, refer to the following topic: Connect an XAF Application to a Database Provider).

Use the ConnectionString attribute in the configuration file (MySolution.Win\App.config).

<?xml version="1.0"?>
  <!-- ... -->
      <add name="ConnectionString" connectionString="Integrated Security=SSPI;MultipleActiveResultSets=True;Data Source=(localdb)\mssqllocaldb;Initial Catalog=Solution111" providerName="System.Data.SqlClient" />
      <!-- ... -->

Add Entities to the UI

To display your entities in the Navigation System, follow the steps below.

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

    using System;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using DevExpress.Persistent.Base;
    using DevExpress.Persistent.BaseImpl.EF;
    // ...
    public class Contact : BaseObject {
        public virtual String FullName { get; set; }
    // Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
  2. Add the new entity to the solution’s DbContext in the MySolution.Module\BusinessObjects\MySolutionDbContext.cs file.

// ...
using Microsoft.EntityFrameworkCore;

namespace YourApplicationName.Module.BusinessObjects;
// ...
public class YourApplicationNameEFCoreDbContext : DbContext {
    public YourApplicationNameEFCoreDbContext(DbContextOptions<YourApplicationNameEFCoreDbContext> options) : base(options) { }
    // ...
    public DbSet<Contact> Contacts { get; set; }

    // ...

You can review the MainDemo.NET.EFCore demo application that ships with XAF. You can find the demo in the following folder: %PUBLIC%\Documents\DevExpress Demos 23.1\Components\XAF\MainDemo.NET.EFCore.

See Also