The following example shows how to add the Entity Framework data source to the Web Dashboard’s list of data sources available to end-users. In this example, the DashboardEFDataSource class is used to establish a connection to the Entity Framework data source.
using System;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
namespace WebDashboard_EFDataSource {
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication {
protected void Application_Start() {
DashboardConfig.RegisterService(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
DevExpress.Web.ASPxWebControl.CallbackError += Application_Error;
}
protected void Application_Error(object sender, EventArgs e) {
Exception exception = System.Web.HttpContext.Current.Server.GetLastError();
//TODO: Handle Exception
}
}
}
namespace WebDashboard_EFDataSource.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("SalesPerson")]
public partial class SalesPerson
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int OrderID { get; set; }
[StringLength(15)]
public string Country { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(10)]
public string FirstName { get; set; }
[Key]
[Column(Order = 2)]
[StringLength(20)]
public string LastName { get; set; }
[Key]
[Column(Order = 3)]
[StringLength(40)]
public string ProductName { get; set; }
[Key]
[Column(Order = 4)]
[StringLength(15)]
public string CategoryName { get; set; }
public DateTime? OrderDate { get; set; }
[Key]
[Column(Order = 5, TypeName = "smallmoney")]
public decimal UnitPrice { get; set; }
[Key]
[Column(Order = 6)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public short Quantity { get; set; }
[Key]
[Column(Order = 7)]
public float Discount { get; set; }
[Column("Extended Price", TypeName = "money")]
public decimal? Extended_Price { get; set; }
[Key]
[Column("Sales Person", Order = 8)]
[StringLength(31)]
public string Sales_Person { get; set; }
}
}
@Html.DevExpress().Dashboard(settings => {
settings.Name = "Dashboard";
settings.Width = Unit.Percentage(100);
settings.Height = Unit.Percentage(100);
settings.ClientSideEvents.BeforeRender = "onBeforeRender";
}).GetHtml()
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DashboardWeb.Mvc;
using DevExpress.DataAccess.EntityFramework;
using System.Web.Routing;
using WebDashboard_EFDataSource.Models;
namespace WebDashboard_EFDataSource {
public static class DashboardConfig {
public static void RegisterService(RouteCollection routes) {
routes.MapDashboardRoute("dashboardControl");
DashboardFileStorage dashboardFileStorage = new DashboardFileStorage("~/App_Data/Dashboards");
DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage);
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
// Registers an Entity Framework data source.
DashboardEFDataSource efDataSource = new DashboardEFDataSource("EF Data Source");
efDataSource.ConnectionParameters = new EFConnectionParameters(typeof(NorthwindDataModel));
dataSourceStorage.RegisterDataSource("efDataSource", efDataSource.SaveToXml());
DashboardConfigurator.Default.SetDataSourceStorage(dataSourceStorage);
}
}
}
namespace WebDashboard_EFDataSource.Models {
using System.Data.Entity;
public partial class NorthwindDataModel : DbContext {
public NorthwindDataModel()
: base("name=NWindConnectionString") {
}
public virtual DbSet<SalesPerson> SalesPersons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<SalesPerson>()
.Property(e => e.UnitPrice)
.HasPrecision(10, 4);
modelBuilder.Entity<SalesPerson>()
.Property(e => e.Extended_Price)
.HasPrecision(19, 4);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebDashboard_EFDataSource.Controllers {
public class HomeController : Controller {
public ActionResult Index() {
return View();
}
}
}
Imports System.Data.Entity.Spatial
Imports System.ComponentModel.DataAnnotations.Schema
Imports System.ComponentModel.DataAnnotations
Imports System.Collections.Generic
Imports System
Namespace WebDashboard_EFDataSource.Models
<Table("SalesPerson")> _
Partial Public Class SalesPerson
<Key, Column(Order := 0), DatabaseGenerated(DatabaseGeneratedOption.None)> _
Public Property OrderID() As Integer
<StringLength(15)> _
Public Property Country() As String
<Key, Column(Order := 1), StringLength(10)> _
Public Property FirstName() As String
<Key, Column(Order := 2), StringLength(20)> _
Public Property LastName() As String
<Key, Column(Order := 3), StringLength(40)> _
Public Property ProductName() As String
<Key, Column(Order := 4), StringLength(15)> _
Public Property CategoryName() As String
Public Property OrderDate() As Date?
<Key, Column(Order := 5, TypeName := "smallmoney")> _
Public Property UnitPrice() As Decimal
<Key, Column(Order := 6), DatabaseGenerated(DatabaseGeneratedOption.None)> _
Public Property Quantity() As Short
<Key, Column(Order := 7)> _
Public Property Discount() As Single
<Column("Extended Price", TypeName := "money")> _
Public Property Extended_Price() As Decimal?
<Key, Column("Sales Person", Order := 8), StringLength(31)> _
Public Property Sales_Person() As String
End Class
End Namespace
@Html.DevExpress().Dashboard(settings => {
settings.Name = "Dashboard";
settings.Width = Unit.Percentage(100);
settings.Height = Unit.Percentage(100);
settings.ClientSideEvents.BeforeRender = "onBeforeRender";
}).GetHtml()
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Namespace WebDashboard_EFDataSource.Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As ActionResult
Return View()
End Function
End Class
End Namespace
Imports System
Imports System.Web.Http
Imports System.Web.Mvc
Imports System.Web.Routing
Namespace WebDashboard_EFDataSource
' Note: For instructions on enabling IIS6 or IIS7 classic mode,
' visit http://go.microsoft.com/?LinkId=9394801
Public Class MvcApplication
Inherits System.Web.HttpApplication
Protected Sub Application_Start()
DashboardConfig.RegisterService(RouteTable.Routes)
AreaRegistration.RegisterAllAreas()
GlobalConfiguration.Configure(AddressOf WebApiConfig.Register)
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
RouteConfig.RegisterRoutes(RouteTable.Routes)
ModelBinders.Binders.DefaultBinder = New DevExpress.Web.Mvc.DevExpressEditorsBinder()
AddHandler DevExpress.Web.ASPxWebControl.CallbackError, AddressOf Application_Error
End Sub
Protected Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim exception As Exception = System.Web.HttpContext.Current.Server.GetLastError()
'TODO: Handle Exception
End Sub
End Class
End Namespace
Imports System.Data.Entity
Namespace WebDashboard_EFDataSource.Models
Partial Public Class NorthwindDataModel
Inherits DbContext
Public Sub New()
MyBase.New("name=NWindConnectionString")
End Sub
Public Overridable Property SalesPersons() As DbSet(Of SalesPerson)
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
modelBuilder.Entity(Of SalesPerson)().Property(Function(e) e.UnitPrice).HasPrecision(10, 4)
modelBuilder.Entity(Of SalesPerson)().Property(Function(e) e.Extended_Price).HasPrecision(19, 4)
End Sub
End Class
End Namespace
Imports System.Web.Routing
Imports DevExpress.DashboardCommon
Imports DevExpress.DashboardWeb
Imports DevExpress.DashboardWeb.Mvc
Imports DevExpress.DataAccess.EntityFramework
Imports WebDashboard_EFDataSource.Models
Namespace WebDashboard_EFDataSource
Public NotInheritable Class DashboardConfig
Private Sub New()
End Sub
Public Shared Sub RegisterService(ByVal routes As RouteCollection)
routes.MapDashboardRoute("dashboardControl")
Dim dashboardFileStorage As New DashboardFileStorage("~/App_Data/Dashboards")
DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage)
Dim dataSourceStorage As New DataSourceInMemoryStorage()
' Registers an Entity Framework data source.
Dim efDataSource As New DashboardEFDataSource("EF Data Source")
efDataSource.ConnectionParameters = New EFConnectionParameters(GetType(NorthwindDataModel))
dataSourceStorage.RegisterDataSource("efDataSource", efDataSource.SaveToXml())
DashboardConfigurator.Default.SetDataSourceStorage(dataSourceStorage)
End Sub
End Class
End Namespace