[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Common Concepts
 [Expand]MVVM Framework
 [Collapse]Controls and Libraries
   Reporting
  [Expand]Data Grid
  [Expand]Ribbon, Bars and Menu
  [Expand]Charts Suite
  [Expand]Pivot Grid
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Tree List
  [Expand]Gauge Controls
  [Expand]Map Control
  [Expand]Layout Management
  [Expand]Windows Modern UI
  [Expand]Printing-Exporting
  [Expand]Data Editors
  [Expand]Navigation Controls
  [Expand]Spell Checker
  [Expand]Property Grid
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Gantt Control
  [Expand]Diagram Control
  [Expand]Windows and Utility Controls
   Dialogs, Notifications and Panels
  [Collapse]Scheduler (legacy)
   [Expand]Overview
   [Collapse]Getting Started
     Lesson 1 - Create an Application using the Template Gallery or Instant Layout Assistant (legacy)
     Lesson 2 - Provide Bar UI for a Scheduler (legacy)
     Lesson 3 - Provide Ribbon UI for a Scheduler (legacy)
     Lesson 4 - Use the DateNavigator Control (legacy)
     Lesson 5 - Bind a Scheduler to Entity Framework Data Source (legacy)
     Lesson 6 - Bind a Scheduler to MS SQL Server Database (legacy)
     Lesson 7 - Create a Custom Edit Appointment Form Using the MVVM Pattern (legacy)
     Lesson 8 - Create a Custom Appointment Recurrence Dialog Using the MVVM Pattern (legacy)
     Lesson 9 - Create a Custom Edit Appointment Form (legacy)
   [Expand]Fundamentals
   [Expand]Visual Elements
   [Expand]Examples
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Lesson 5 - Bind a Scheduler to Entity Framework Data Source (legacy)

Note

You are viewing documentation for the legacy WPF Scheduler control. If you're starting a new project, we strongly recommend that you use a new control declared in the DevExpress.Xpf.Scheduling namespace. If you decide to upgrade an existing project in order to switch to the updated scheduler control, see the Migration Guidelines document.

In this lesson, you will learn how to create a simple scheduling application with the SchedulerControl, and how to bind it to the Entity Framework data source. Note that the SchedulerControl can be bound to a variety of other data sources.

This topic also describes how to map all the required data fields for scheduler appointments, and how to populate the scheduler data at runtime.

This lesson consists of the following sections.

Expanded Create a Model

  1. Open the WPF application with the SchedulerControl, which was created in Lesson 3 of the current tutorial.
  2. The scheduler can work without the data source - in unbound mode, but any changes you make will not persist between sessions. To save changes, the scheduler should be bound to an external data source. This example implements Entity Framework objects as the data source. The Entity Framework is Microsoft's recommended data access technology for new applications. To use the Entity Framework, install the Entity Framework NuGet package. To do this, right-click on the project in the Solution Explorer, and select Manage NuGet Packages.
  3. In the Manage NuGet Packages dialog, select the Browse tab and choose the EntityFramework package. Click Install.
  4. Add the ADO .NET Entity Data Model to the project. To accomplish this, perform the following.

    1. Right-click on the project name. Select Add, then New Item.
    2. Select Data, ADO .NET Entity Data Model and enter MySchedulerModel for the name.
    3. In the Entity Data Model Wizard, select the Empty Code First model and click Finish. The model template is created.
  5. In the model template, replace the public class MyEntity class definition with the code for EFAppointment and EFResource classes, as follows.

    Note that the integer type of the Color field implies that the ResourceStorage.ColorSaving property is set to the ColorSavingType.ArgbColor value.

Expanded Modify XAML to Specify Mappings and Bindings

  • Change the XAML code of the SchedulerControl in the MainWindow page to specify the data source and mappings for the AppointmentStorage and ResourceStorage components, as in the following code snippet.

Expanded Add Code that Handles Data Interaction

  1. The Entity Framework requires a context object. It should be the class that descends from the System.Data.Entity.DbContext class. Name it MySchedulerModel and implement it as follows.

    Provide a database initializer that populates the database with data immediately after the database is generated. The SchedulerDBInitializer initializer class inherits from the DropCreateDatabaseIfModelChanges interface (this strategy means that it will automatically delete and recreate the database, and optionally seed it with new data if a model changes). To initially populate a Resource table, override the SchedulerDBInitializer's Seed method.

    To accomplish this task, implement the SchedulerDBInitializer class as follows.

  2. Declare a new variable of the MySchedulerModel type in the MainWindow's code behind file and initialize it. It will be used to hold the data context.
  3. Subscribe to the MainWindow's Loaded event in XAML. In the code behind file (MainWindow.xaml.cs), load data from the database and set the data context for the AppointmentStorage and the ResourceStorage objects. Note that to use the Load extension method of the class you should declare the System.Data.Entity namespace with the using (C#) or Imports (Visual Basic) directive. The Local property of the class should be used for data binding.

    To post data back to the database, use the SaveChanges method of the System.Data.Entity.DbContext<EFAppointment> class. Subscribe to the SchedulerStorage.AppointmentsInserted, SchedulerStorage.AppointmentsChanged, SchedulerStorage.AppointmentsDeleted events and call the SaveChanges method in the event handler.

    The code is shown below.

  4. Override the OnClosing method of the MainWindow class for correct disposal of the data context, as in the following code snippet.

Expanded Run the Project

  • Compile and run the application. If it is run for the first time, then the MySchedulerModel database is created automatically in the (LocalDb)\v11.0 server and the Resource table is populated with data.

    The application window looks as follows.

Is this topic helpful?​​​​​​​