How to: Use the Entity Framework Data Model Located in an External Assembly
3 min 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. However, if your existing application is based on the Entity Framework 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.
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 v19.2 XAF Solution Wizard. Select Entity Framework Code First at the Choose ORM step and click Finish.
Reference the external assembly that contains the Entity Framework 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, inherit the DbContext declared in the external assembly.
Add required entity types from the external assembly to the existing DbContext located in the BusinessObjects/MySolutionDbContext.cs(MySolutionDbContext.vb) file.
Run the Module Designer. In the Exported Types section, expand the Referenced Assemblies node. Then, expand the node that corresponds to the external assembly. Select entities to add and press the SPACEBAR or use the Use Type in Application command from the context menu. The selected entities will be marked in bold. This means that they have been added to the Application Model, and will take part in the UI construction process.
Alternatively, you can add entities in code. Open the Module.cs (Module.vb) file and add the required entity types to the ModuleBase.AdditionalExportedTypes collection in the module's constructor.
Rebuild your solution, so that the changes made in the Designer 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.
Ensure that the correct DbContext type is passed to the application's Object Space Provider in the CreateDefaultObjectSpaceProvider method located in WinApplication.cs (WinApplication.vb) and WebApplication.cs (WebApplication.vb) files. For details, refer to the Specify the Entity Container (Context) section of the Use the Entity Framework Data Model topic.