windowsforms-11910-controls-and-libraries-scheduler-data-binding-data-sources-entity-framework-code-first.md
A SchedulerDataStorage can be bound to a data source created using the Entity Framework Code First approach. The following code demonstrates how to implement appointment and resource objects.
class EFAppointment
{
[Key]
public int UniqueID {get;set;}
[Required]
public int Type {get;set;}
[Required]
public DateTime StartDate {get;set;}
[Required]
public DateTime EndDate {get;set;}
public bool AllDay {get;set;}
public string Subject {get;set;}
public string Location {get;set;}
public string Description { get; set; }
public int Status {get;set;}
public int Label {get;set;}
public string ResourceIDs {get;set;}
public string ReminderInfo {get;set;}
public string RecurrenceInfo {get;set;}
}
Friend Class EFAppointment
<Key> _
Public Property UniqueID() As Integer
<Required> _
Public Property Type() As Integer
<Required> _
Public Property StartDate() As Date
<Required> _
Public Property EndDate() As Date
Public Property AllDay() As Boolean
Public Property Subject() As String
Public Property Location() As String
Public Property Description() As String
Public Property Status() As Integer
Public Property Label() As Integer
Public Property ResourceIDs() As String
Public Property ReminderInfo() As String
Public Property RecurrenceInfo() As String
End Class
class EFResource
{
[Key]
public int UniqueID { get; set; }
public int ResourceID { get; set; }
public string ResourceName { get; set; }
public byte[] Image { get; set; }
public int Color
{
get
{
return ColorEx.ToArgb();
}
set
{
ColorEx = new MyColor(value);
}
}
public MyColor ColorEx { get; set; }
}
class MyColor
{
public byte A { get; set; }
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public MyColor() { }
public MyColor(byte a, byte r, byte g, byte b) { A = a; R = r; G = g; B = b; }
public MyColor(Color color) { A = color.A; R = color.R; G = color.G; B = color.B; }
public MyColor(int argb)
{
byte[] bytes = BitConverter.GetBytes(argb);
A = bytes[0];
R = bytes[1];
G = bytes[2];
B = bytes[3];
}
public Color ToColor() { return Color.FromArgb(A, R, G, B); }
public static MyColor FromColor(Color color) { return new MyColor(color.A, color.R, color.G, color.B); }
public int ToArgb()
{
byte[] bytes = new byte[] { A, R, G, B };
return BitConverter.ToInt32(bytes, 0);
}
}
Friend Class EFResource
<Key> _
Public Property UniqueID() As Integer
Public Property ResourceID() As Integer
Public Property ResourceName() As String
Public Property Image() As Byte()
Public Property Color() As Integer
Get
Return ColorEx.ToArgb()
End Get
Set(ByVal value As Integer)
ColorEx = New MyColor(value)
End Set
End Property
Public Property ColorEx() As MyColor
End Class
Friend Class MyColor
Public Property A() As Byte
Public Property R() As Byte
Public Property G() As Byte
Public Property B() As Byte
Public Sub New()
End Sub
Public Sub New(ByVal a As Byte, ByVal r As Byte, ByVal g As Byte, ByVal b As Byte)
Me.A = a
Me.R = r
Me.G = g
Me.B = b
End Sub
Public Sub New(ByVal color As Color)
A = color.A
R = color.R
G = color.G
B = color.B
End Sub
Public Sub New(ByVal argb As Integer)
Dim bytes() As Byte = BitConverter.GetBytes(argb)
A = bytes(0)
R = bytes(1)
G = bytes(2)
B = bytes(3)
End Sub
Public Function ToColor() As Color
Return Color.FromArgb(A, R, G, B)
End Function
Public Shared Function FromColor(ByVal color As Color) As MyColor
Return New MyColor(color.A, color.R, color.G, color.B)
End Function
Public Function ToArgb() As Integer
Dim bytes() As Byte = { A, R, G, B }
Return BitConverter.ToInt32(bytes, 0)
End Function
End Class
The DbContext class descendant is required to retrieve data from the database. It is implemented as follows:
class SchedulerContext:DbContext
{
public DbSet<EFAppointment> EFAppointments { get; set; }
public DbSet<EFResource> EFResources { get; set; }
}
Friend Class SchedulerContext
Inherits DbContext
Public Property EFAppointments() As DbSet(Of EFAppointment)
Public Property EFResources() As DbSet(Of EFResource)
End Class
Before binding to a data source we have to map data fields to appointment and resource properties. The code that creates the required mappings is shown below.
this.schedulerStorage1.Appointments.Mappings.AllDay = "AllDay";
this.schedulerStorage1.Appointments.Mappings.AppointmentId = "UniqueID";
this.schedulerStorage1.Appointments.Mappings.Description = "Description";
this.schedulerStorage1.Appointments.Mappings.End = "EndDate";
this.schedulerStorage1.Appointments.Mappings.Label = "Label";
this.schedulerStorage1.Appointments.Mappings.Location = "Location";
this.schedulerStorage1.Appointments.Mappings.RecurrenceInfo = "RecurrenceInfo";
this.schedulerStorage1.Appointments.Mappings.ReminderInfo = "ReminderInfo";
this.schedulerStorage1.Appointments.Mappings.ResourceId = "ResourceIDs";
this.schedulerStorage1.Appointments.Mappings.Start = "StartDate";
this.schedulerStorage1.Appointments.Mappings.Status = "Status";
this.schedulerStorage1.Appointments.Mappings.Subject = "Subject";
this.schedulerStorage1.Appointments.Mappings.Type = "Type";
Me.schedulerStorage1.Appointments.Mappings.AllDay = "AllDay"
Me.schedulerStorage1.Appointments.Mappings.AppointmentId = "UniqueID"
Me.schedulerStorage1.Appointments.Mappings.Description = "Description"
Me.schedulerStorage1.Appointments.Mappings.End = "EndDate"
Me.schedulerStorage1.Appointments.Mappings.Label = "Label"
Me.schedulerStorage1.Appointments.Mappings.Location = "Location"
Me.schedulerStorage1.Appointments.Mappings.RecurrenceInfo = "RecurrenceInfo"
Me.schedulerStorage1.Appointments.Mappings.ReminderInfo = "ReminderInfo"
Me.schedulerStorage1.Appointments.Mappings.ResourceId = "ResourceIDs"
Me.schedulerStorage1.Appointments.Mappings.Start = "StartDate"
Me.schedulerStorage1.Appointments.Mappings.Status = "Status"
Me.schedulerStorage1.Appointments.Mappings.Subject = "Subject"
Me.schedulerStorage1.Appointments.Mappings.Type = "Type"
this.schedulerStorage1.Resources.Mappings.Caption = "ResourceName";
this.schedulerStorage1.Resources.Mappings.Color = "Color";
this.schedulerStorage1.Resources.Mappings.Id = "ResourceID";
this.schedulerStorage1.Resources.Mappings.Image = "Image";
Me.schedulerStorage1.Resources.Mappings.Caption = "ResourceName"
Me.schedulerStorage1.Resources.Mappings.Color = "Color"
Me.schedulerStorage1.Resources.Mappings.Id = "ResourceID"
Me.schedulerStorage1.Resources.Mappings.Image = "Image"
Handle the Form.Load event to create the database, fill it with initial data and specify appointment and resource binding sources.
Database.SetInitializer(new SchedulerContextSeedInilializer());
context = new SchedulerContext();
context.Database.Initialize(false);
context.EFAppointments.Load();
context.EFResources.Load();
eFAppointmentBindingSource.DataSource = context.EFAppointments.Local.ToBindingList<EFAppointment>();
eFResourceBindingSource.DataSource = context.EFResources.Local.ToBindingList<EFResource>();
Database.SetInitializer(New SchedulerContextSeedInilializer())
context = New SchedulerContext()
context.Database.Initialize(False)
context.EFAppointments.Load()
context.EFResources.Load()
eFAppointmentBindingSource.DataSource = context.EFAppointments.Local.ToBindingList()
eFResourceBindingSource.DataSource = context.EFResources.Local.ToBindingList()
Not the use of the System.Data.Entity.DbExtensions.ToBindingList<T> method as the source for data binding.
The SaveChanges method of DbContext descendant - the SchedulerContext object - is used to commit changes to the data source. Subscribe to the SchedulerDataStorage.AppointmentsInserted, SchedulerDataStorage.AppointmentsChanged and the SchedulerDataStorage.AppointmentsDeleted events to save the SchedulerContext object.
To exclude the identity field from the fields being committed to the data source, set the AppointmentStorage.CommitIdToDataSource property to false.