How to: Use the Entity Framework Core Data Model Located in an External Assembly
- 2 minutes to read
If you have a non-XAF application, and want to develop an XAF application that utilizes the same database, you can generate business classes for an existing database to achieve this task (see Reverse Engineering). However, if your existing application is based on the Entity Framework Core data model, you can reuse this model in XAF to avoid code duplication. This topic describes how to use the data model located within an external assembly.
Note
If the external assembly is an XAF Module, then you do not need to follow this topic. Entities declared within XAF modules are automatically recognized and added to the Application Model.
- Create a new XAF solution using the DevExpress v24.1 XAF Template Gallery (.NET Core). Select Entity Framework Core at the Choose ORM step and click Finish.
- Reference the external assembly that contains the Entity Framework Core data model to be used.
A DbContext class is required to use entities from the external assembly. To declare it, do one of the following.
- In the module project (MySolution.Module), inherit the DbContext declared in the external assembly.
- Add required entity types from the external assembly to the existing DbContext located in the BusinessObjects\MySolutionEFCoreDbContext.cs file.
Open the MySolution.Module\Module.cs file and add the required entity types to the ModuleBase.AdditionalExportedTypes collection in the module’s constructor:
using MyDataModelLibrary; // ... public sealed partial class MySolutionModule : ModuleBase { public MySolutionModule() { // ... AdditionalExportedTypes.Add(typeof(Employee)); AdditionalExportedTypes.Add(typeof(Task)); } }
Rebuild your solution, so that the changes made in the Module are loaded to the Application Model, and run the Model Editor. Make sure that the entities added in the previous step are available in the BOModel node.
Add navigation items for the added entities by following the steps described in the Add an Item to the Navigation Control tutorial.
- Ensure that the correct DbContext type is passed to the application’s Object Space Provider in the CreateDefaultObjectSpaceProvider method located in the WinApplication.cs or BlazorApplication.cs file. For details, refer to the Specify the Entity Container (Context) section of the Use the Entity Framework Core Data Model topic.