Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to bind the GridView with the Entity Framework in a regular and Database Server modes

$
0
0

This example is an illustration of the KA18615: How to bind MVC GridView Extension with Entity Framework in a server mode KB Article. Please refer to the article for an explanation.
This example illustrates how to use the GridViewExtension.BindToEF method for binding the MVC GridView Extension with the EF data content in:

- The Grid mode. All data shaping operations are performed on the WebServer/Grid side (for a small volume of data). Note that a table from the EF context used as the grid's Model should be explicitly evaluated, for example, via the ToList method:

[C#]
NorthwindContextdb=newNorthwindContext();

[C#]
publicActionResultGridViewPartial(){returnPartialView(db.Orders.ToList());}

[C#]
@Html.DevExpress().GridView(settings=>{...settings.CallbackRouteValues=new{Controller="EFGridMode",Action="GridViewPartial"};}).Bind(Model).GetHtml()

- The Database Server mode. All data shaping operations are performed on the database server side. This mode is effective when it is necessary to maintain a large volume of data only, for example 100k records. Since this mode creates special LINQ queries, this mode may be not so effective when operating with small portion of data:

[C#]
NorthwindContextdb=newNorthwindContext();

[C#]
publicActionResultGridViewPartial(){returnPartialView(db.Orders);}

[C#]
@Html.DevExpress().GridView(settings=>{...settings.CallbackRouteValues=new{Controller="EFDatabaseServerMode",Action="GridViewPartial"};}).BindToEF(string.Empty,string.Empty,(s,e)=>{e.KeyExpression="OrderID";e.QueryableSource=Model;}).GetHtml()

This example operates the Northwind SQL Compact demo database. The data Model is defined via the EF Code First approach. Since tables’ column names contain spaces, it is necessary to define the corresponding mappings at the DBContext level (via the OnModelCreating method). In most cases, this step is no longer required.

Question Comments

Added By: David Kolb at: 6/13/2013 6:40:35 PM    

For EF 5.0, I get the following message from the view after returning EntityServerModeSource - "The model item passed into the dictionary is of type 'DevExpress.Data.Linq.EntityServerModeSource', but this dictionary requires a model item of type 'System.Collections.IEnumerable'."

Added By: Bernd Coch 1 at: 5/3/2016 8:52:15 AM    Hello,

I am using DevExpress v15.2 with Entity Framework 6. The application is using WebForms, so I cannot use the example below with QueryableSourc.

I create an IQueryable from the EF context as follows:
[VB.NET]
Dim connectionString = ConfigurationManager.ConnectionStrings("ConnectionString_1").ToString()Dim invDataContext = New Inv.InvDataContext(connectionString)

I need to set additional predicates (filters) to the IQueryable that I use as a datasource.  These come on top of any filters that the grid might create.
I obtain an instance of !Queryable from the context and add custom filters

[VB.NET]
' Predicate depends on user access rights, it is independent of the grid filter and always applied invDataContext.MyTable.Where(predicate)
Then I assign this source to the AspGridView and call Databind()

[VB.NET]
xGrid_Forecast.DataSource = source xGrid_Forecast.DataBind()
Then I am getting the error 

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported

What is the right way to bind an IQueryable to the  AspxGridView?  

The option of calling ToList() BEFORE assigning the IQueryable to the source is not viable, because it would mean that the entire table (> 200000 rows) would be fetched before the pager settings of the grid are applied.

Thank you for your help

Bernd
Added By: Mike (DevExpress Support) at: 5/3/2016 10:41:05 AM    

Hello Bernd,

I've created a separate ticket on your behalf (T374412: ASPxGridView - How to use DataSource as custom IQueryable from EF). It has been placed in our processing queue and will be answered shortly.


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>