Server Mode with Entity Framework
- 3 minutes to read
The PivotGridControl automatically operates in server mode if it is bound to the Entity Framework data source. This example demonstrates how to generate model and mapping information, and bind the Pivot Grid control to the data source with the Entity Framework.
Creating Data Classes
Add ADO.NET Entity Data Model to the project.
Create a model in the EF Designer based on an existing database. You can choose the model type, a database connection string, the objects to include in the model. The following image gallery shows the Entity Data Model Wizard:
When you click Finish at the last Entity Data Model Wizard screen, the following warning appears:
The created model looks as follows:
Important
After you create a data model, rebuild the solution.
Binding PivotGridControl to the EntityServerModeSource component
Drag the EntityServerModeSource component and drop it onto the Form.
Note
Alternatively, you can create the EntityServerModeSource in code at runtime.
Specify the type of objects retrieved from a data source using the EntityServerModeSource.ElementType and EntityServerModeSource.KeyExpression properties.
Specify the queryable source with the EntityServerModeSource.QueryableSource property.
Bind the Pivot Grid control to the EntityServerModeSource component.
The resulting code looks as follows:
using DevExpress.Data.Linq; using DevExpress.XtraEditors; using System; namespace EntityFrameworkServerModeExample { public partial class Form1 : XtraForm { EntityServerModeSource entityServerModeSource; public Form1() { InitializeComponent(); entityServerModeSource = new EntityServerModeSource { ElementType = typeof(EntityInvoice), KeyExpression = "OrderId" }; NWEntities context = new NWEntities( //Enable query logging. context.Database.Log = Console.Write; entityServerModeSource.QueryableSource = context.EntityInvoices; } private void Form1_Load(object sender, EventArgs e) { pivotGridControl1.DataSource = entityServerModeSource; } } }
Run the project. The PivotGridControl works in server mode. You can see the generated SQL statements in the Visual Studio Output window.
Note
The complete sample project PivotGridControl and Entity Framework - a Server Mode Example is available in the DevExpress Examples repository..