Skip to main content
All docs
V25.1
.NET 8.0+

DevExpress v25.1 Update — Your Feedback Matters

Our What's New in v25.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 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: MySolution.Module\BusinessObjects\MySolutionDbContext.cs

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

namespace MySolution.Module.BusinessObjects;

public class MySolutionDesignTimeDbContextFactory : DesignTimeDbContextFactory<MySolutionDbContext> {
    protected override string ConnectionString
        => throw new InvalidOperationException("Connection string not specified. Specify connection string to your database here.");
    // Specify the connection string used by the application and defined in the config file, either MySolution.Blazor.Server/appsettings.json or MySolution.Win/App.config
}

[TypesInfoInitializer(typeof(DbContextTypesInfoInitializer<MySolutionDbContext>))]
public class MySolutionDbContext : DbContext {
    public MySolutionDbContext(DbContextOptions<MySolutionDbContext> 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) {
        base.OnModelCreating(modelBuilder);
        // ...
    }
}

Important

XAF’s EF Core implementation does not support the following data model configurations: entities with composite/compound keys (multiple properties as an entity key), keyless entity types, and owned entity types. In such scenarios, inherit your EF Core entity from XAF’s BaseObject class or define your own class with a single Guid, numeric (Int32, Int64 ), or String primary key. You can also use non-persistent objects to integrate XAF UI with legacy systems.

#Specify the Database Connection

Important

XAF requires Multiple Active Result Sets (MARS) for EF Core applications connected to Microsoft SQL Server. Do not remove MultipleActiveResultSets=true; and do not set it to false in the connection string. For databases that do not support MARS, XAF has an internal mechanism to function without MARS. To enable it, include Persist Security Info=True in the connection string.

Specify the database connection in the ConnectionString section in the configuration file. For additional information, refer to the following topic: Connect an XAF Application to a Database Provider).

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

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

    C#
    using System;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using DevExpress.Persistent.Base;
    using DevExpress.Persistent.BaseImpl.EF;
    // ...
    [DefaultClassOptions]
    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.

    C#
    // ...
    using Microsoft.EntityFrameworkCore;
    
    namespace MySolution.Module.BusinessObjects;
    // ...
    public class MySolutionDbContext : DbContext {
      public MySolutionDbContext(DbContextOptions<MySolutionDbContext> 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 25.1\Components\XAF\MainDemo.NET.EFCore.

See Also