dashboard-401406-web-dashboard-integrate-dashboard-component-aspnet-web-forms-dashboard-control-register-default-data-sources-entity-framework-data-source.md
This topic shows how to add the DashboardEFDataSource to an in-memory data source storage, and make it available to users.
The ASP.NET Web Forms Dashboard supports the following Entity Framework version: Entity Framework 5.0 and higher.
Specify a connection to the database in Web.config.
In this example, the connection name is OrdersContext. The connection supplies the dashboard with data from the MS SQL Server database file (NWind.mdf).
<configuration>
<connectionStrings>
<add name="OrdersContext" connectionString="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\NWind.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The Web Dashboard supports the following data providers: Custom Connection Strings for Data Sources.
Right-click the project and select Add → New Item…. In the opened dialog, select ADO.NET Entity Data Model.
Specify OrdersContext as the model name and select the Code First from database model type in the invoked wizard.
Select the added OrdersContext string as a data connection for the created model.
On the next page, specify which tables and views to include in your model.
Note that the Entity Framework context class should be public.
In this tutorial, the data model is based on the OrderDetail table. The code below shows the generated data model.
C# :
namespace WebFormsDashboardDataSources {
using System.Data.Entity;
public partial class OrdersContext: DbContext {
public OrdersContext()
: base("name=OrdersContext") {
}
public virtual DbSet<OrderDetail> OrderDetails { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<OrderDetail>()
.Property(e => e.UnitPrice)
.HasPrecision(10, 4);
}
}
}
namespace WebFormsDashboardDataSources {
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class OrderDetail
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int OrderID { get; set; }
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public short Quantity { get; set; }
[Column(Order = 2, TypeName = "smallmoney")]
public decimal UnitPrice { get; set; }
[Column(Order = 3)]
public float Discount { get; set; }
[Column(Order = 4)]
[StringLength(40)]
public string ProductName { get; set; }
[StringLength(217)]
public string Supplier { get; set; }
}
}
Visual Basic :
Imports System.Data.Entity
Namespace WebFormsDashboardDataSources
Public Partial Class OrdersContext
Inherits DbContext
Public Sub New()
MyBase.New("name=OrdersContext")
End Sub
Public Overridable Property OrderDetails As DbSet(Of OrderDetail)
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
modelBuilder.Entity(Of OrderDetail)().Property(Function(e) e.UnitPrice).HasPrecision(10, 4)
End Sub
End Class
End Namespace
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema
Namespace WebFormsDashboardDataSources
Public Partial Class OrderDetail
<Key>
<Column(Order:=0)>
<DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property OrderID As Integer
<Column(Order:=1)>
<DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property Quantity As Short
<Column(Order:=2, TypeName:="smallmoney")>
Public Property UnitPrice As Decimal
<Column(Order:=3)>
Public Property Discount As Single
<Column(Order:=4)>
<StringLength(40)>
Public Property ProductName As String
<StringLength(217)>
Public Property Supplier As String
End Class
End Namespace
For example, your ASPX page contains the ASPxDashboard control which unique identifier is ASPxDashboardEf:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="position: absolute; top: 0; bottom: 0; left: 0; right: 0">
<dx:ASPxDashboard ID="ASPxDashboardEf" runat="server" Width="100%" Height="100%">
</dx:ASPxDashboard>
</div>
</form>
</body>
</html>
You can define the Entity Framework Data Source in the code-behind page that has the .aspx.cs or .aspx.vb extension depending on the language used:
Note
A code-behind page is one of the variants where you can register the data sources. For example, you can also register them in the Global.asax.cs (Global.asax.vb) file.
using System;
using System.Web.Hosting;
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.EntityFramework;
namespace WebFormsDashboardDataSources.Pages {
public partial class EFDashboard : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
// ...
// Create a data source storage.
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
// Register an Entity Framework data source.
DashboardEFDataSource efDataSource = new DashboardEFDataSource("EF Core Data Source");
efDataSource.ConnectionParameters = new EFConnectionParameters(typeof(OrdersContext));
dataSourceStorage.RegisterDataSource("efDataSource", efDataSource.SaveToXml());
// Set the configured data source storage.
ASPxDashboardEf.SetDataSourceStorage(dataSourceStorage);
ASPxDashboardEf.InitialDashboardId = "dashboardEf";
}
}
}
Imports System
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports DevExpress.DataAccess.EntityFramework
Namespace WebFormsDashboardDataSources.Pages
Public Partial Class EFDashboard
Inherits Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' ...
' Create a data source storage.
Dim dataSourceStorage As DataSourceInMemoryStorage = New DataSourceInMemoryStorage()
' Register an Entity Framework data source.
Dim efDataSource As DashboardEFDataSource = New DashboardEFDataSource("EF Core Data Source")
efDataSource.ConnectionParameters = New EFConnectionParameters(GetType(OrdersContext))
dataSourceStorage.RegisterDataSource("efDataSource", efDataSource.SaveToXml())
' Set the configured data source storage.
ASPxDashboardEf.SetDataSourceStorage(dataSourceStorage)
ASPxDashboardEf.InitialDashboardId = "dashboardEf"
End Sub
End Class
End Namespace
The EF Core Data Source is now available in the Web Dashboard:
Users can bind dashboard items to data in the Web Dashboard’s UI.
The example shows how to make a set of data sources available for users in the Web Dashboard application.
View Example: How to Register Data Sources for ASP.NET Web Forms Dashboard Control