Entity Framework 4 “Code First” CTP


There are all sorts of blog posts flying about at the moment regarding EF4’s Code First CTP: –

Since they all cover the finer details in excruiating detail, I wanted to just talk about my brief experience with it yesterday and today.

Firstly, installation is easy – just download the CTP, it installs a folder in Program Files that you reference in your .NET projects (Microsoft.Data.Entity.CTP).

Actually using it is extremely easy and quick, as the above blogs attest to – you can just create POCOs, put the type within your context and off you go – it really is that easy. Unfortunately  I could find no way of actually viewing the database tables that the framework creates, as I’m using SQL Compact 4 (Code First does not (yet?) work with Sql Compact 3.5).

Anyway – just an example of how easy it was to create a simple two-tier inheritance hierarchy…

image

Here’s the code for it:

class EmployeeDbContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Person> People { get; set; }
}

class Person
{
    public int PersonId { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }
    public string Name { get { return String.Format("{0} {1}", Forename, Surname); } }
}

class Employee : Person
{
    public string Role { get; set; }
    public int Salary { get; set; }
}

class Customer : Person
{
    public int OrderCount { get; set; }
}

Not a lot of code really – and that’s about all that’s required in terms of set-up of code. Here’s an example of my using it – pretty standard Entity Framework really aside from the second line that tells EF to automatically update the database schema if you make changes to the object model: –

static void Main (string[] args)
{
    Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
    Database.SetInitializer(new RecreateDatabaseIfModelChanges<EmployeeDbContext>());
    var context = new EmployeeDbContext();

    AddNewEmployee(context);
    AddNewCustomer(context);
    context.SaveChanges();

    Console.WriteLine("Employees");
    foreach (var employee in context.Employees)
        Console.WriteLine(employee.Name);

    Console.WriteLine();
    Console.WriteLine("Customers");
    foreach (Customer customer in context.Customers)
        Console.WriteLine(customer.Name);

    Console.WriteLine();
    Console.WriteLine("People");
    foreach (var person in context.People)
        Console.WriteLine(person.Name);

    Console.Read();
}

private static void AddNewCustomer (EmployeeDbContext employeeDbContext)
{
    employeeDbContext.Customers.Add(new Customer
    {
        Forename = "John",
        Surname = "Smith",
        OrderCount = 10
    });
}

private static void AddNewEmployee (EmployeeDbContext employeeDbContext)
{
    employeeDbContext.Employees.Add(new Employee
    {
        Forename = "Isaac",
        Surname = "Abraham",
        Role = "Developer",
        Salary = 2000
    });
}

A just to prove that EF understands the inheritance hierarchy, here’s a screenshot with the output from the code above:

image

The nice thing about this is the ease of creation – you can be up and running with a database in literally minutes. Just hitting F5 with the code above will implicitly create a new database and the schema to match the object model. Nice.

I’m not sure how easily you could push updates post-deployment e.g. you’ve got a production database and want to amend some fields etc. – I believe that this sort of thing is still to come – but it’s still quite impressive.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s