Bind Pivot Grid to Data using Entity Framework (Code First)
- 4 minutes to read
The DevExpress MVC PivotGrid supports binding to a data source using the Entity Framework ORM.
If you do not yet have a database, you can code your own classes and properties that correspond to tables and columns and use them with the Entity Framework. The Entity Framework can automatically create the database for you, or drop and recreate it if the model changes. This development approach is called “Code First”.
This topic describes how to bind the MVC PivotGrid to a data source using the Entity Framework and the “Code First” development approach.
Note
Project requirements Before you start, make sure you have the Entity Framework installed. Learn more about how to get the Entity Framework from the following tutorial: Get Entity Framework.
Your project should be prepared for using DevExpress MVC Extensions. See this topic to learn how to prepare your project: Integration into an ASP.NET MVC Project.
To implement this kind of data binding, you need to perform the following steps.
- Step 1. Create a data model and a data context
- Step 2. Add a connection string
- Step 3. Add the PivotGrid to the required View
Step 1. Create a data model and a data context
The data model class (the “Product” class in this topic) is used to represent data records in a database. Each instance of a data model class will correspond to a row within a database table, and each property of the data model class will map to a column within a database table.
The image below demonstrates how the model class properties are represented within the PivotGrid.
The data context class (“ProductDbContext” in this topic) represents the Entity Framework database context, which handles fetching, storing, and updating data model class instances in a database. The data context class derives from the System.Data.Entity.DbContext base class provided by the Entity Framework.
Add the data model class with the required properties and a data context class to the data model code, as shown in the code sample below.
using System.Data.Entity;
namespace Demo1.Models
{
public class Product
{
public int Id { get; set; }
public string ProductName { get; set; }
public string CategoryName { get; set; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
}
public class ProductDbContext : DbContext
{
public DbSet<Product> Customers { get; set; }
}
}
Step 2. Add a connection string
Create a connection string that points to your data context class and place it in the <connectionStrings> element in the Web.config file. You can learn more about connection strings from the following topic: Connection Strings.
The following is an example of a connection string for the data model created in the previous step.
<connectionStrings>
<add name="ProductDbContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Products.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Note
Build the application before going on to the next step.
Step 3. Add the PivotGrid to the required View
Open the required View file (Index.cshtml in this topic), right-click the desired location in the Code Editor to display the Shortcut menu, and click Insert DevExpress MVC Extension…
The Insert DevExpress Extension wizard opens. Navigate to the tab with the Data extensions and select PivotGrid. 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 and select the fields you want to add to the PivotGrid.
Click the “Insert” button and the PivotGrid will be added into your project. See this topic to learn more about the MVC Wizard: Insert DevExpress MVC Extension Wizard.
After these manipulations, Insert Extension Wizard generates a partial view with the PivotGrid‘s settings and inserts necessary code into the corresponding controller class. As a result, you have a fully functional MVC PivotGrid bound to a data source using the Entity Framework.