wpf-115305-controls-and-libraries-scheduler-examples-how-to-bind-scheduler-to-data-using-the-entity-framework-code-first-approach.md
This example describes how to bind the Scheduler to a data source using the Entity Framework Code First approach.
Create a new WPF Application project and open the MainWindow.xaml file in the Visual Studio Designer.
Add the SchedulerControl object to your project. You can do this by dragging the SchedulerControl item from the DX.25.2: Scheduling Toolbox tab to the canvas.
Right-click the SchedulerControl object and select Layout | Reset All in the context menu to make the SchedulerControl fill the entire window.
Create a new file (for example, Data.cs(vb)) and add the data classes to it.
public class AppointmentEntity {
public int Id { get; set; }
public string Subject { get; set; }
public string Description { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public int AppointmentType { get; set; }
public string RecurrenceInfo { get; set; }
public int ResourceId { get; set; }
public int Label { get; set; }
}
public class ResourceEntity {
public int Id { get; set; }
public string Description { get; set; }
}
Public Class AppointmentEntity
Public Property Id() As Integer
Public Property Subject() As String
Public Property Description() As String
Public Property Start() As Date
Public Property End() As Date
Public Property AppointmentType() As Integer
Public Property RecurrenceInfo() As String
Public Property ResourceId() As Integer
Public Property Label() As Integer
End Class
Public Class ResourceEntity
Public Property Id() As Integer
Public Property Description() As String
End Class
Note
Before creating a Data Context, make sure that the Entity Framework package is added to your Visual Studio. Refer to the Get Entity Framework article for details on how to install the Entity Framework package.
Define a database context (a DBContext class descendant) that represents a session with the database and allows you to query and save class instances.
public class SchedulingContext : DbContext {
public DbSet<AppointmentEntity> Appointments { get; set; }
public DbSet<ResourceEntity> Resources { get; set; }
}
Public Class SchedulingContext
Inherits DbContext
Public Property Appointments() As DbSet(Of AppointmentEntity)
Public Property Resources() As DbSet(Of ResourceEntity)
End Class
Add the SchedulingContext initializer that seeds the database with sample data when the application starts for the first time.
public class SchedulingContextInitializer : DropCreateDatabaseIfModelChanges<SchedulingContext> {
protected override void Seed(SchedulingContext context) {
base.Seed(context);
context.Resources.Add(DataHelper.Personal());
context.Resources.Add(DataHelper.Education());
context.Resources.Add(DataHelper.Work());
context.Appointments.AddRange(DataHelper.Gym());
context.Appointments.Add(DataHelper.Dentist());
context.Appointments.Add(DataHelper.Dinner());
context.Appointments.Add(DataHelper.Disneyland());
context.Appointments.Add(DataHelper.RR());
context.Appointments.Add(DataHelper.DayOff());
context.Appointments.Add(DataHelper.SecondShift());
context.Appointments.Add(DataHelper.ConferenceCompanyMeeting());
context.Appointments.Add(DataHelper.ConferenceCustomerRetentionReview());
context.Appointments.Add(DataHelper.ConferenceDatabaseAndWebsiteReview());
context.Appointments.Add(DataHelper.ConferenceWeeklyMeeting());
context.Appointments.Add(DataHelper.TrainingFrenchLesson());
context.Appointments.Add(DataHelper.TrainingGermanLesson());
context.Appointments.Add(DataHelper.TrainingTrainStaffOnNewRemoteControls());
context.SaveChanges();
}
}
Public Class SchedulingContextInitializer
Inherits DropCreateDatabaseIfModelChanges(Of SchedulingContext)
Protected Overrides Sub Seed(ByVal context As SchedulingContext)
MyBase.Seed(context)
context.Resources.Add(DataHelper.Personal())
context.Resources.Add(DataHelper.Education())
context.Resources.Add(DataHelper.Work())
context.Appointments.AddRange(DataHelper.Gym())
context.Appointments.Add(DataHelper.Dentist())
context.Appointments.Add(DataHelper.Dinner())
context.Appointments.Add(DataHelper.Disneyland())
context.Appointments.Add(DataHelper.RR())
context.Appointments.Add(DataHelper.DayOff())
context.Appointments.Add(DataHelper.SecondShift())
context.Appointments.Add(DataHelper.ConferenceCompanyMeeting())
context.Appointments.Add(DataHelper.ConferenceCustomerRetentionReview())
context.Appointments.Add(DataHelper.ConferenceDatabaseAndWebsiteReview())
context.Appointments.Add(DataHelper.ConferenceWeeklyMeeting())
context.Appointments.Add(DataHelper.TrainingFrenchLesson())
context.Appointments.Add(DataHelper.TrainingGermanLesson())
context.Appointments.Add(DataHelper.TrainingTrainStaffOnNewRemoteControls())
context.SaveChanges()
End Sub
End Class
Register the initializer on the application startup callback.
public partial class App : Application {
protected override void OnStartup(StartupEventArgs e) {
Database.SetInitializer<SchedulingContext>(new SchedulingContextInitializer());
base.OnStartup(e);
}
}
Partial Public Class App
Inherits Application
Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs)
Database.SetInitializer(Of SchedulingContext)(New SchedulingContextInitializer())
MyBase.OnStartup(e)
End Sub
End Class
In the Solution Explorer, right-click to invoke the context menu and select Add | Class…. In the invoked Add New Item window, assign the SchedulingViewModel name to the class.
Add the following code to handle data interaction:
public class SchedulingViewModel : ViewModelBase {
SchedulingContext context;
public ObservableCollection<AppointmentEntity> Appts { get { return context.Appointments.Local; } }
public ObservableCollection<ResourceEntity> Calendars { get { return context.Resources.Local; } }
}
Public Class SchedulingViewModel
Inherits ViewModelBase
Private context As SchedulingContext
Public ReadOnly Property Appts() As ObservableCollection(Of AppointmentEntity)
Get
Return context.Appointments.Local
End Get
End Property
Public ReadOnly Property Calendars() As ObservableCollection(Of ResourceEntity)
Get
Return context.Resources.Local
End Get
End Property
End Class
public ICommand SaveCommand {
get {
return new DelegateCommand(() => {
context.SaveChanges();
});
}
}
Public ReadOnly Property SaveCommand() As ICommand
Get
Return New DelegateCommand(Sub()
context.SaveChanges()
Status = $"Changes are saved to database. {Date.Now.ToLongTimeString()}."
End Sub)
End Get
End Property
public SchedulingViewModel() {
context = new SchedulingContext();
context.Appointments.Load();
context.Resources.Load();
}
Public Sub New()
context = New SchedulingContext()
context.Appointments.Load()
context.Resources.Load()
End Sub
<Window.DataContext>
<local:SchedulingViewModel />
</Window.DataContext>
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand Command="{Binding SaveCommand}" EventName="AppointmentAdded" />
<dxmvvm:EventToCommand Command="{Binding SaveCommand}" EventName="AppointmentEdited" />
<dxmvvm:EventToCommand Command="{Binding SaveCommand}" EventName="AppointmentRemoved" />
<dxmvvm:EventToCommand Command="{Binding SaveCommand}" EventName="AppointmentRestored" />
</dxmvvm:Interaction.Behaviors>
<dxsch:SchedulerControl.DataSource>
<dxsch:DataSource AppointmentsSource="{Binding Appts}"
ResourcesSource="{Binding Calendars}">
<dxsch:DataSource.ResourceMappings>
<dxsch:ResourceMappings Caption="Description"
Id="Id" />
</dxsch:DataSource.ResourceMappings>
<dxsch:DataSource.AppointmentMappings>
<dxsch:AppointmentMappings Description="Description"
End="End"
Id="Id"
LabelId="Label"
RecurrenceInfo="RecurrenceInfo"
ResourceId="ResourceId"
Start="Start"
Subject="Subject"
Type="AppointmentType" />
</dxsch:DataSource.AppointmentMappings>
</dxsch:DataSource>
</dxsch:SchedulerControl.DataSource>
The following image demonstrates the result:
View Example: How to bind Scheduler to data using the Entity Framework Code First approach