Skip to main content

Entity Framework Code First

  • 5 minutes to read

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.

Appointments

View Example

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;}
}

Resources

View Example

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);
    }

}

DbContext

The DbContext class descendant is required to retrieve data from the database. It is implemented as follows:

View Example

class SchedulerContext:DbContext
{
    public DbSet<EFAppointment> EFAppointments { get; set; }
    public DbSet<EFResource> EFResources { get; set; }
}

Mappings

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.

View Example

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";

View Example

this.schedulerStorage1.Resources.Mappings.Caption = "ResourceName";
this.schedulerStorage1.Resources.Mappings.Color = "Color";
this.schedulerStorage1.Resources.Mappings.Id = "ResourceID";
this.schedulerStorage1.Resources.Mappings.Image = "Image";

Database Initialization

Handle the Form.Load event to create the database, fill it with initial data and specify appointment and resource binding sources.

View Example

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>();

Not the use of the System.Data.Entity.DbExtensions.ToBindingList<T> method as the source for data binding.

Database Commit

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.