How To: Generate Data Access Layer with Entity Framework Code First

  • 6 minutes to read

This topic gives a step-by-step description of how to generate the Data Access Layer based on a predefined data model with the Entity Framework.

Create an empty WPF application (you can use the Template Gallery to do this), or open an existing project and follow the steps below.

The complete sample project is available in the DevExpress Code Examples database at: http://www.devexpress.com/example=T319497.

Define Data Structure

Create a new file (for example, Model.cs(vb)) and add the following data classes to it.

public class Department {
    public Department() {
        Courses = new HashSet<Course>();
    }
    public int DepartmentID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Course> Courses { get; set; }
}
public class Course {
    public int CourseID { get; set; }
    public string Title { get; set; }
    public int DepartmentID { get; set; }
    public virtual Department Department { get; set; }
}
public class Employee {
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public int DepartmentID { get; set; }
    public virtual Department Department { get; set; }
}

Add NuGet Package

Add the Entity Framework NuGet package to your project. Right-click the project in the Solution Explorer and select the Manage NuGet Packages option.

NOTE

If you do not have the Manage NuGet Packages option, install the latest version of NuGet.

sc-codefirst-05-NuGetMenu

Input the Entity Framework text to the search box, find the EntityFramework package, click Install and accept the License Agreement.

sc-codefirst-10-NuGet

Define a Database Context

Define a database context that represents a session with the database and allows you to query and save data.

public class DepartmentContext : DbContext {
    public DbSet<Department> Departments { get; set; }
    public DbSet<Course> Courses { get; set; }
    public DbSet<Employee> Employees { get; set; }
}

The defined data context represents the Data Access layer in our application.

Add Sample Data

Add the DepartmentContext initializer that will seed the database with sample data when the application starts for the first time.

public class DepartmentContextInitializer : DropCreateDatabaseIfModelChanges<DepartmentContext> {
    protected override void Seed(DepartmentContext context) {
        base.Seed(context);

        Department department1 = new Department { Name = "Music" };
        Department department2 = new Department { Name = "Journalism" };
        Department department3 = new Department { Name = "Management" };
        context.Departments.Add(department1);
        context.Departments.Add(department2);
        context.Departments.Add(department3);

        context.Courses.AddRange(new[] {
            new Course { Title = "First", Department = department1 },
            new Course { Title = "Second", Department = department1 },
            new Course { Title = "Third", Department = department1 },

            new Course { Title = "First", Department = department2 },
            new Course { Title = "Second", Department = department2 },
            new Course { Title = "Third", Department = department2 },

            new Course { Title = "First", Department = department3 },
            new Course { Title = "Second", Department = department3 },
            new Course { Title = "Third", Department = department3 },
        });

        context.Employees.AddRange(new[] {
            new Employee { Name = "Frankie West PhD", Department = department1 },
            new Employee { Name = "Jett Mitchell", Department = department1 },
            new Employee { Name = "Garrick Stiedemann DVM", Department = department1 },
            new Employee { Name = "Hettie Runte", Department = department1 },
            new Employee { Name = "Gabe Flatley", Department = department2 },
            new Employee { Name = "Zetta Beatty", Department = department2 },
            new Employee { Name = "Ms. Luis Jewess", Department = department2 },
            new Employee { Name = "Jefferey Legros III", Department = department3 },
            new Employee { Name = "Margaretta Roberts", Department = department3 },
        });

        context.SaveChanges();
    }
}

Then, register it on the application startup callback.

public partial class App : Application {
    protected override void OnStartup(StartupEventArgs e) {
        Database.SetInitializer<DepartmentContext>(new DepartmentContextInitializer());
        base.OnStartup(e);
    }
}

What is Next?

When, the Data Access Layer is generated, you can use our Scaffolding Wizard to generate the other parts of a database-oriented application. The UI Generation topic describes how to do this step-by-step.