Binding to Data via Entity Framework or LINQ to SQL (Database First)
- 3 min to read
The DevExpress MVC GridView supports binding to an existing data source using the LINQ or the Entity Framework.
If you already have a database, the LINQ to SQL ORM Framework or the Entity Framework ORM can automatically generate a data model that consists of classes and properties that correspond to existing database objects such as tables and columns. This development approach is called "Database First".
This topic describes how to bind the MVC GridView to a data source using the "Database First" development approach.
Your project should be prepared for using DevExpress MVC Extensions. See the following topic to learn how to prepare your project: Integration into an ASP.NET MVC Project.
To use the Server Mode with Entity Framework 6+, it is necessary to use special options on the application startup. To learn more, see the Knowledge Base Article: Is it possible to use the Entity Framework 6 and Server Mode together.
To implement this kind of data binding, you need to perform the following steps.
Step 1. Create a data model
Use the required ORM framework to reverse engineer a model from an existing database.
LINQ to SQL ORM Framework
Create a LINQ to SQL data model. The following MSDN topic demonstrates how to add LINQ to SQL data model classes via the O/R designer: How to: Add LINQ to SQL Classes to a Project (O/R Designer).
Refer to the following MSDN Learning Center article to learn more about the LINQ to SQL ORM Framework: LINQ to SQL.
Entity Framework ORM
Before you start, make sure you have the Entity Framework installed. Learn more about how to get the Entity Framework from the following MSDN tutorial: Get Entity Framework.
Create an Entity Framework data model. The following walkthrough demonstrates how to reverse engineer a model from an existing database: Entity Framework Database First.
Rebuild the solution before going on to the next step.
Step 2. Add the GridView to the required View
Open the required View file (Index.cshtml in this topic), focus on the position in the code where you would like to insert the GridView, and right-click -> select "Insert DevExpress MVC Extension v19.2…"
The Insert Extension Wizard opens. Navigate to the tab with the Data extensions and select the GridView. Define the extension name and the partial view name. In the 'Model class' item select the data model class, in the 'Data context class' item select the data context class, select the columns you wish to display within the GridView and select the key field.
Click the 'Insert' button, and the GridView will be added into your project. See this topic to learn more about the MVC Wizard: Insert DevExpress MVC Extension Wizard.
After performing these operations, Insert Extension Wizard generates a partial view that contains the GridView's settings and inserts the necessary code into the corresponding controller class. As a result, you have a fully functional MVC GridView bound to an existing data source.