1005009: Need to create documentation for MySQL server integrate with DataGrid #7442
+767
−1,012
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
layout: post
title: MySQL Server Data Binding in Blazor DataGrid Component using Entity Framework | Syncfusion
description: Learn about connecting MySQL Server with Entity Framework Core and binding data to Syncfusion Blazor DataGrid with complete CRUD operations.
platform: Blazor
control: DataGrid
documentation: ug
Connecting MySQL Server data to Blazor DataGrid Component
The Syncfusion® Blazor DataGrid component supports binding data from a MySQL Server database using Entity Framework Core (EF Core). This modern approach provides a more maintainable and type-safe alternative to raw SQL queries.
Entity Framework Core is an Object-Relational Mapper (ORM) that simplifies database operations by:
Prerequisites
Ensure the following software and packages are installed before proceeding:
MySQL server environment
This project uses a MySQL database with Entity Framework Core (Pomelo) to store and manage payment transaction records and to support server-side CRUD, searching, filtering, sorting, grouping, and paging for the transactions table.
Step 1: Create database and table in MySQL server
Create a MySQL database named transactiondb and a transactions table to store payment records. Run the below script on MySQL Workbench.
The above script stores the transaction records in the transactions table within the transactiondb database.
Step 2: Install required NuGet packages
Open the Package Manager Console in Visual Studio and install the necessary packages:
Alternatively, use NuGet Package Manager UI:
Now the required packages are installed for the server setup.
Step 3: Create the data model
Create a Data folder and add Transaction.cs file. Now define a model class named "TransactionModel" with appropriate properties and data annotations that represents the database entity.
Now Model class for the transactions table has been created.
Step 4: Configure the DbContext
The
DbContextis the Entity Framework Core class that manages database connections and translates application-level operations into MySQL commands.Create the Data/TransactionDbContext.cs file and define the DbContext class "TransactionDbContext". This class defines entity configurations of MySQL database.
Step 5: Configure the connection string
The connection string specifies the information that connects the database like MySQL server location, credentials, and database name in the runtime.
Update appsettings.json file with MySQL connection string:
{ "ConnectionStrings": { "DefaultConnection": "Server=localhost;Port=3306;Database=transactiondb;User=root;Password=your_password;" }, }Connection String Components:
Now data base connection has been completed.
Step 6: Create the repository pattern class
Create a repository class in Data/TransactionRepository.cs to handle all data operations with Entity Framework Core:
Step 7: Register DbContext
Register APIs via dependency injection.
Configure Entity Framework Core in Program.cs file:
Now Services registration have been completed.
Integrating Syncfusion Blazor DataGrid
Step 1: Install required NuGet packages and register Syncfusion services
Open the Package Manager Console in Visual Studio and install the necessary packages:
Registers the Syncfusion component services required by the DataGrid at runtime and exposes grid APIs via dependency injection.
Configure Syncfusion services in Program.cs:
Add required namespaces in Components/_Imports.razor:
Add stylesheet and script resources in Components/App.razor
Service registration and namespace imports have been completed.
Step 2: Binding data from MySQL Server using CustomAdaptor
The Syncfusion® Blazor DataGrid can bind data from a MySQL Server database using DataManager and set the Adaptor property to CustomAdaptor for scenarios that require full control over data operations.
The Custom Adaptor serves as the bridge between DataGrid UI interactions and MySQL Server database operations. When users interact with the grid (search, filter, sort, page), the adaptor intercepts these requests and executes corresponding MySQL operations.
Implement custom adaptor
Create a Custom Adaptor class in Components/Pages/Home.razor that bridges DataGrid actions with MySQL Server operations:
Custom Adaptor methods reference:
Bind the adaptor to the DataGrid markup in Home.razor:
Data flow architecture:
The Custom Adaptor implementation centralizes all database logic, enabling consistent MySQL execution, error handling, and performance optimization across all grid operations.
Handling data operations
The Syncfusion® Blazor DataGrid supports server-side operations such as searching, filtering, sorting, paging, and aggregating when using the CustomAdaptor by overriding the
ReadAsyncmethod of theDataAdaptorabstract class.The
DataManagerRequestobject supplies the necessary details for each operation, and the built‑in methods in theDataOperationsandDataUtilclasses apply those details accordingly.Common methods in dataoperations
Handling searching operation
Enables keyword-based query across configured fields, allowing the grid to delegate search criteria to the adaptor for efficient server-side filtering. The built-in
PerformSearchingmethod of theDataOperationsclass applies search criteria from theDataManagerRequestto the data source.Enable the search toolbar in DataGrid markup:
Handling filtering operation
Provides column-level criteria evaluation so the adaptor can restrict datasets at the source for precise, efficient retrieval. The built-in
PerformFilteringmethod in theDataOperationsclass applies filter criteria from theDataManagerRequestto the data collection.Enable filtering in DataGrid markup:
Handling sorting operation
Enables deterministic record ordering by delegating sort descriptors to the adaptor for database-optimized sorting. The built-in
PerformSortingmethod in theDataOperationsclass applies sort criteria from theDataManagerRequestto the data collection.Enable sorting in DataGrid markup:
Handling aggregation operation
Aggregate functions compute summary statistics across datasets without requiring row-level retrieval, enabling efficient calculation of totals, averages, and counts at the database server level. The built-in
PerformAggregationmethod in the DataUtil class calculates aggregate values based on the criteria specified in theDataManagerRequest.Enable aggregates in DataGrid markup:
Handling paging operation
Paging divides large result sets into fixed-size pages, reducing memory consumption and improving client-side responsiveness by retrieving only the requested page from the server.The built-in
PerformSkip,PerformTakemethods in theDataOperationsclass apply paging criteria from theDataManagerRequestto the data collection.Enable paging in DataGrid markup:
Handling grouping operation
Grouping hierarchically organizes records by specified column values, enabling data summarization and nested record visualization while reducing query complexity through server-side grouping operations.The built-in
Groupmethod in theDataUtilclass applies grouping logic based on the configuration in theDataManagerRequest.Handling CRUD operations
Custom Adaptor methods enable users to create, read, update, and delete records directly from the DataGrid. Each operation calls corresponding data layer methods in TransactionRepository.cs to execute MySQL commands.
Insert operation
Record insertion enables users to create new transactions directly from the DataGrid interface; the adaptor intercepts the insertion request, applies business logic validation, and persists the new record to the MySQL Server database.
Implement
InsertAsyncto handle new record creation:In Data/TransactionRepository.cs, implement the insert method:
Record insertion has been configured; the new transaction is now persisted to the database and reflected in the grid.
Update operation
Record modification enables users to edit transaction attributes directly within the DataGrid; the adaptor captures the edited row, validates changes, and synchronizes modifications to the MySQL Server database while maintaining data integrity.
Implement
UpdateAsyncto handle record modifications:In Data/TransactionRepository.cs, implement the update method:
Record updates have been configured; modifications are now synchronized to the database and reflected in the grid UI.
Delete operation
Record deletion enables users to remove transactions directly from the DataGrid; the adaptor intercepts the deletion request, executes corresponding MySQL DELETE statements, and removes the record from both the database and grid display.
Implement
RemoveAsyncto handle record deletion:In Data/TransactionRepository.cs, implement the delete method:
Record deletion has been implemented; transactions are now removed from the database and the grid UI reflects the changes immediately.
Batch update operation
Batch operations aggregate multiple insertions, updates, and deletions into a single request, reducing network overhead and enabling transactional consistency by persisting all changes atomically to the MySQL Server database.
Implement
BatchUpdateAsyncto process multiple record changes in a single request:Batch operations have been configured; the adaptor now supports bulk modifications with atomic database synchronization. All CRUD operations are now fully implemented, enabling comprehensive data management capabilities within the Blazor DataGrid.
Running the sample
Build and run the application:
Navigate to the application URL (e.g., https://localhost:5001) to view the DataGrid with MySQL Server data binding.