Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

How to synchronize database data with ABP vNext entity objects and code generation tools

Tech May 16 1

When using EF Core's Code First approach, if you directly add new tables or stored procedures in the database, you need to manually reverse-engineer these database changes in your project code to keep the Code First code synchronized with the database. This situation can be addressed through the following two steps:

  1. Create Models from Existing Database: Use Entity Framework Core's Scaffold-DbContext command to generate entity models and DbContext from the database. This operation generates corresponding domain models and configuration code based on the tables and views in the database. The command is as follows:
Scaffold-DbContext "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force

Replace "YourConnectionString" with your data base connection string, and "Models" with the directory where you want the generated classes to be placed. Use the -Force parameter to overwrite existing files if necessary.

  1. Stored Procedure Mapping: EF Core does not automatically generate C# methods for stored procedures. You need to manually add code to call stored procedures. Use the FromSqlRaw or ExecuteSqlRaw method of DbContext to execute stored procedures and map the results to domain model entities, for example:
var result = context.YourEntityModel.FromSqlRaw("EXEC YourStoredProcedure @param1, @param2", parameters).ToList();

Thus, although synchronization is not fully automatic, using Scaffold-DbContext to assist in model generation, combined with manually adding stored procedure call methods, can still conveniently synchronize database chenges to the project code.

The key point to remember is that when working in Code First mode, you should generally avoid making structural changes directly on the database side. Instead, you should synchronize the database structure by changing domain models and using EF Core Migrations. This helps maintain consistency and traceability between code and database structure. Making changes directly in the database and then reverse-engineering them in to the code should be considered a temporary approach for special cases.

The ABP vNext framework community has indeed developed some entity class code generation tools to help you generate domain model code from the database. Below are a few highly-starred GitHub repositories that may contain tools you find interesting:

  1. NameIsBad/abp-vue

    • Description: ABP vNext + vue3 (vben) + code generator
    • Stars: 30
    • This repository provides a code generator for ABP vNext projects integrated with Vue3.
  2. WuLex/AbpVnextGenerator

    • Description: Abp Vnext Basic Code Generator
    • Stars: 29
    • This tool provides basic ABP vNext code generation capabilities, helping you generate entity class code, etc.
  3. neozhu/abpvnextsmartcodegenerator

    • Description: Visual Studio.net 2017 extension for ABP vNext code generator archetype
    • Stars: 24
    • If you are using Visual Studio 2017, this extension might be suitable for you. It is a code generator written for ABP vNext.

Before choosing to use these tools, it is recommended to check the update dates and community feedback to ensure they meet your project requirements. When creating entity classes, these tools can automatically generate corresponding entity class code and configuration files based on database tables, helping you integrate into ABP projects more quickly. If you have specific questions about the tools, consult the documentation of the corresponding repository or seek help from the community.

Tags: ABP vNext

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.