Cocktail for Windows Store – Part 3

One style of Entity Framework development is known as Code-First. With Code-First style, you’d define the schema using .NET classes and properties. This style of development makes it easy to keep the data model under source control.

Entity Framework comes with a powershell script that detects the schema changes as you add/modify the classes, and generates the necessary upgrade scripts for you.

Our project involves an assignment tracker for schools. Students can be assigned one or more assignments in subjects they are enrolled to. Assignments are created by the teachers who teach the relevant subject.


// Schema.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace AssignmentTracker
{
    using IdeaBlade.Aop;

    [ProvideEntityAspect]
    public class Assignment
    {
        [Key]
        public int Id { get; set; }

        [MaxLength]
        public string Description { get; set; }

        [ForeignKey("Student")]
        public int StudentId { get; set; }

        [ForeignKey("Subject")]
        public int SubjectId { get; set; }

        public DateTime? DateDue { get; set; }

        [Association("Assignment_StudentSystemUser", "StudentId", "Id")]
        public Student Student { get; set; }

        [Association("Assignment_Subject", "SubjectId", "Id")]
        public Subject Subject { get; set; }
    }

    [ProvideEntityAspect]
    public class Subject
    {
        public Subject()
        {
        }

        [Key]
        public int Id { get; set; }

        [Required]
        [MaxLength(255)]
        public string Description { get; set; }

        [ForeignKey("Teacher")]
        public int TeacherId { get; set; }

        [Association("Subject_Teacher", "TeacherId", "Id")]
        public Teacher Teacher { get; set; }

        public IList<Student> Students { get; set; }
    
    }

    [ProvideEntityAspect]
    public class SystemUser
    {
        [Key]
        public int Id { get; set; }

        [MaxLength(100)]
        [Required]
        public string UserName { get; set; }

        [MaxLength(100)]
        public string FullName { get; set; }

    }

    public class Student : SystemUser
    {
        public Student()
        {
            //Subjects = new RelatedEntityList<Subject>();
        }

        [Required]
        [MaxLength(15)]
        public string StudentNumber { get; set; }

        public ICollection<Subject> Subjects { get; set; }
    }

    public class Teacher : SystemUser
    {
        public virtual IList<Subject> Subjects { get; set; }
    }
}

Schema.cs is added to AssignmentTracker.Server.dll and added as a link to the AssignmentTracker.RT project. This is the main value from DevForce, the same code that is used on the server is deployed on the client.