Binding to Entity Framework Core
- 3 minutes to read
Entity Framework (EF) Core is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology. To bind a DevExpress data-aware control to an EF Core source, you need to manually create a Data Model first. Then, utilize the Data Source Configuration Wizard.
Database and Data Model
Depending on the database provider you choose, you may be required to execute different steps and install different NuGet packages. Below are a few Microsoft tutorials on creating code-first Models or EF Models based on existing databases.
The example below illustrates how to generate an EF model from a sample Adventure Works 2014 database stored at the SQL Express server.
- Create a new WinForms solution using the DevExpress Template Gallery. Choose the "Grid based UI" template to create a Data Grid control hosted within a skinnable XtraForm.
Make sure the target .NET framework of your project is version 4.5 or higher. To check and modify the target framework version, go to "Project | <Your_Project_Name> Properties", then use the "Target framework" editor to select the required version.
For Entity Framework Core 2.0, perform additional steps before proceeding.
- Make sure you have Visual Studio 2017 (version 15.3.1 and newer) installed - older VS versions are not supported.
- Install .NET Core SDK.
Open Visual Studio settings ("Debug | Options"), switch to the "NuGet Package Manager | General" tab and choose "PackageReference" as the default package management format.
Alternatively, you can add the following line to your .csproj/.vbproj file.
Enable automatic binding redirect generation by adding the following line to the .csproj/.vbproj file.
See the official Microsoft Issues with .NET Standard 2.0 with .NET Framework & NuGet blogpost for the information on resolving possible issues that may occur when using EF Core 2.0.
Open the PMC ("Tools | NuGet Package | Package Manager Console") and enter the following lines one-by-one to install the required packages.
Microsoft SQL Server database provider
Entity Framework Core Package Manager Console Tools
Refer to the Database Providers article to learn how to install NuGet packages for other providers.
In PMC, enter the following line to generate an EF model based on your database. Note that you must replace the connection parameters (Server, Database and login credentials) with your actual values.
Scaffold-DbContext "Server=MYSERVER\SQLEXPRESS;Database=AdventureWorks2014;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer
This will add multiple class files to your solution. The "<Database_Name>Context" class represents a session with the database and allows you to query and save instances of the entity classes. Other files represent individual database tables.
- Rebuild your project.
Invoke the Data Source Configuration Wizard for the Data Grid control and select "Entity Framework (EF) Core". If the Data Model was generated correctly, you will see it under the "Data Sources" header. Select it and click "Next" to proceed.
Select the required connection type.
Select the required database table. For all connection types except for direct binding, you can also apply sorting by the selected data field.
Run the application to see the result.