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

Implement a Data Model: Basics

  • 4 minutes to read

This lesson explains how to implement entity classes for your application and describes the basics of automatic user interface construction based on data.

During this lesson, you will do the following:

  • Add a simple entity class.
  • Display the entity class in the application’s navigation control.

Entity classes do not depend on the application UI. Implement them in a platform-independent module project. This way, other XAF or non-XAF applications can share entities.

Inherit your entity classes from the base persistent class DevExpress.Persistent.BaseImpl.EF.BaseObject. The BaseObject class implements the IXafEntityObject and IObjectSpaceLink interfaces. This means that CRUD operations for the declared objects are available automatically and you don’t need to implement them in your code.

For additional information on these concepts, refer to the following topic: Views.

#Step-by-Step Instructions

  1. Expand the MySolution.Module project and right-click the Business Objects folder. Choose Add | Class…. Specify Employee.cs as the new class name and click Add. Replace the auto-generated code with the following class declaration:

    C#
    using DevExpress.Persistent.BaseImpl.EF;
    
    namespace MySolution.Module.BusinessObjects;
    
    public class Employee : BaseObject {
    
        public virtual String FirstName { get; set; }
    
        public virtual String LastName { get; set; }
    
        public virtual String MiddleName { get; set; }
    
    }
    
  2. Go to the MySolution.Module\BusinessObjects\MySolutionDbContext file and add the following property to the MySolutionEFCoreDbContext entity container class:

    using MySolution.Module.BusinessObjects;
    
    namespace  MySolution.Module.BusinessObjects {
        public class MySolutionEFCoreDbContext : DbContext {
            //...
            public DbSet<Employee> Employees { get; set; }
        }
    }
    

    This property is a collection of Employee class objects. XAF creates a table with the same name in the database and then maps this collection to the table.

    Note

    • If you inherit an entity class from another entity class, you can register both entities in the DbContext class. This way, your application can work with collections of ancestor class objects.
    • 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.

#Propagate Data Model Structure Changes to the Database

Your application now contains data objects. This means that the application requires a database. You have the following options to choose from:

  • Use a DBMS to maintain the database (default option)

    XAF automatically updates database table structure after your data model structure changes. Refer to the following topic for additional information: Automatic Database Schema Update.

  • Use an in-memory database

    This option works best during the development/debugging stage. To enable this option, open the MySolution.Blazor.Server\Startup.cs file, comment the UseSqlServer option, and uncomment the UseInMemoryDatabase option as displayed in the code snippet below. Do the same in the MySolution.Win\Startup.cs file.

    //..
    namespace MySolution.Blazor.Server;
    
    public class Startup {
        public Startup(IConfiguration configuration) {
            Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        // ...
        public void ConfigureServices(IServiceCollection services) {
            services.AddSingleton(typeof(Microsoft.AspNetCore.SignalR.HubConnectionHandler<>), typeof(ProxyHubConnectionHandler<>));
            //...
            services.AddXaf(Configuration, builder => {
                //...
                builder.ObjectSpaceProviders
                    .AddEFCore().WithDbContext<MySolution.Module.BusinessObjects.MySolutionEFCoreDbContext>((serviceProvider, options) => {
                        // ...
                        options.UseInMemoryDatabase("InMemory");
                        string connectionString = null;
                        if(Configuration.GetConnectionString("ConnectionString") != null) {
                            connectionString = Configuration.GetConnectionString("ConnectionString");
                        }
                        //...
                        ArgumentNullException.ThrowIfNull(connectionString);
                        //options.UseSqlServer(connectionString);
                        options.UseChangeTrackingProxies();
                        options.UseObjectSpaceLinkProxies();
                        options.UseLazyLoadingProxies();
                    })
                    // ...
            });
        }
    }
    

#Application Run

  1. Run the application. You can see that the UI did not change. To make the Employee item visible in the application’s navigation control, add the DefaultClassOptionsAttribute attribute to the corresponding class:

    C#
    //...
    
    namespace MySolution.Module.BusinessObjects;
    
    [DefaultClassOptions]
    public class Employee : BaseObject {
    
        //...
    }
    
    //...
    

    With this attribute, you can also use Employee objects as data sources to generate reports. For additional information, refer to the following lesson of this tutorial: Create and Preview a Report.

    To apply each option separately, use the NavigationItemAttribute and VisibleInReportsAttribute attributes.

  2. Run the application. XAF generates a user interface that is based on the specified data structures. The List View displays the collection of objects of the Employee class. Since there are no objects of this type, the Employee List View is empty for now:

    ASP.NET Core Blazor
    XAF ASP.NET Core Blazor App List View
    Windows Forms
    XAF Windows Forms App List View
  3. Click the New button to invoke the Detail View for a new object of the Employee type. XAF renders the properties of the entity class as data fields. The Detail View contains editors for each data field.

    ASP.NET Core Blazor
    ASP.NET Core Blazor List View
    Windows Forms
    XAF Windows Forms List View

#Next Lesson

Extend the Data Model

See Also