The simplest Entity Framework Code First Demo Ever

I’ve downloaded VS2010 SP1 Beta, EntityFramework CTP 5 and SQLCE tooling for VS2010 Beta. Today I want to create the SIMPLEST demo to show it hangs together. It will not take long!!

1. Create a new console app.

2. Add the references to the new EntityFramework assembly and the System.ComponentModel.DataAnnotations assembly.



3. Create two very simple classes, Master and Student. Note the use of the [Key] attribute to define the primary key. The default convention will be that a identity column will be created for these columns.

public class Master
  public int MasterID{get; set;}

  public string Name{get;set;}

  public virtual ICollection<student> Students{get;set;}

public class Student
  public int StudentID{get;set;}

  public string Name{get;set;}

  public Master Master{get;set;}

4. Define the database context that will hold the collections of Masters and Students that define the domain.

public class DojoContext : DbContext
  public DbSet<student> Students { get; set; }
  public DbSet<master> Masters { get; set; }

5. Pretty simple so far huh? Lets get it all working:

class Program
  static void Main(string[] args)
    DbDatabase.DefaultConnectionFactory = 
      new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

    using (DojoContext context = new DojoContext()){
      foreach (Master master in context.Masters){
        Console.WriteLine("Master {0} is in the dojo", master.Name);
        foreach (Student student in master.Students){
          Console.WriteLine("Student {0} is with him", student.Name);

      context.Masters.Add(new Master{ 
                        Name = "fred jones", 
                        Students = new List<student>{
                          new Student{Name="Jenny Townsend"}, 
                          new Student{Name="Jack Philby"}} 

Note how I’ve specified the use of SQL CE 4, but I haven’t had to include a reference to it anywhere. Run this program. On the first time nothing interesting happens. But then run it again, and you’ll see that the Master and the Students have been retrived from the database – persistence in around 10 minutes!!

Where did the data go? If you look in the runtime directory you’ll see a .sdf file as follows:


To see what is this, you can use the SQL Compact Edition 4 Tools for VS2010 SP1 Beta. Simply double click on the sdf and a new Data Connection will appear:


You can the use the familar “Show Table Data” context menu option to edit the table data.


In this short brefing I have covered the barest minimum to create a datastore using the features of the new Entity Framework “Code First”. Like I said, this was the most basic, 100-level example of code first but it should give you an idea of what can be done.

In future posts I will covering database initialization and configuration strategies.

This entry was posted in Design Patterns, Entity Framework, SQL CE. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s