Skip to main content

Entity Model Design and Customization

  • 4 minutes to read

The ExpressEntityMapping Framework supports the Model-First, Database-First, and Code-First approaches to application development, allowing you to create and extend entity models at any development stage of your project. You can design an entity model and its classes with any of these approaches and later fine-tune them in the IDE’s code editor. The sections below provide details on framework features that are intended for each approach.

Model-First Approach

This approach means that you use the Entity Model Designer application shipped with the ExpressEntityMapping Framework to do the following:

  • Visually create and modify an entity model, including entity properties, relationships, inheritance, and mapping information (using context menus, built-in dialogs, and the Object Inspector window);

  • Store an entity model as a file to persist its data between application runs (via the File menu commands);

  • Generate unit files containing entity class declarations and interfaces that enable support for LINQ expressions based on an entity model (via the Model | Generate Code Files menu command). These files include the essential information about your entity model as detailed in the Code-First Approach section below.

Database-First Approach

As with the previous approach, you can use the Entity Model Designer application to design an entity model. However, instead of creating it from scratch, you can create it by importing an existing database. To accomplish this, specify the connection settings for the source database and select the Model | Create Model from Database menu command.

The current Entity Model Designer version replicates the entire database schema by importing tables, columns, and keys as entities, their properties, and relationships (associations) between them. Once created, the entity model is ready for further customization and use in the same manner as with the Model-First approach.

Code-First Approach

This approach provides more granular control over entity models than the other two and means that you create an entity model by reflecting a target data store’s schema in code as follows:

  • Define entities (normally, they correspond to database tables) as classes and table columns as fields and properties in these classes. Class inheritance is fully supported, so you can create base classes and inherit other classes from them;

  • Register classes in the entity model and specify how they and their members map to tables and other data store objects using built-in attributes or an entity manager. Refer to the Entity Class Registration topic to learn about these options;

  • Specify where each entity class descendant stores its data. Storing data in the base class’s table (corresponds to the TdxMapInheritanceType.ParentTable option) requires that you introduce a discriminator column in the base class and provide this class and each entity descendant class with a discriminator column value that uniquely identifies them in the inheritance tree;

  • Identify one or more columns as a primary key for each entity class that maps to a database table. Entity class descendants inherit their primary keys from their ancestors – so you need to identify a primary key only once per inheritance tree;

  • Establish relationships via associations and implement code that initializes fields and properties they use.

After that, you can add the entity model’s classes to your project to start using them and their members in it.

Consider a simple entity model including a Person entity with two attributes: name and age. The following code example uses built-in attributes to declare a class for this entity.

uses
  ..., dxEMF.Core, dxEMF.Attributes, dxEMF.Types;
type
  [Entity]
  [Table('Person')]
  TPerson = class
  strict private
    FId: Integer;
    FAge: Integer;
    FName: string;
  public
    [Column, Key, Generator(TdxGeneratorType.Identity)]
    property Id: Integer read FId write FId;
    [Column]
    property Age: Integer read FAge write FAge;
    [Column]
    property Name: string read FName write FName;
  end;

The class contains three properties, one of which is an auto-incremented primary key (whose values are generated automatically), and the other two store personal information. Call the CreateSchema procedure of a session component connected to an SQLite database and pass the class type as a parameter to create the database whose schema matches this class.

type
  TForm1 = class(TForm)
  // ...
    dxEMFSession: TdxEMFSession;
  // ...
  end;
var
  Form1: TForm1;
implementation
// ...
begin
  // Creating a database and updating its schema to match the TPerson class declaration
  dxEMFSession.DataProvider.Options.AutoCreate := TdxAutoCreateOption.DatabaseAndSchema;
  dxEMFSession.CreateSchema(TPerson);
end;

This creates the following database schema: