EF Feature CTP4 Walkthrough: Productivity Improvements

 


The information in this post is out of date.

Visit msdn.com/data/ef for the latest information on current and past releases of EF.


 

Introduction

We recently announced the release of Entity Framework Feature Community Technology Preview 4 (CTP4) . Feature CTP4 contains a preview of new features that we are considering adding to the core framework in the future and would like to get community feedback on. Feature CTP4 builds on top of the existing Entity Framework 4 (EF4) functionality that shipped with .NET Framework 4.0 and Visual Studio 2010.

If you need assistance with CTP4 we have an Entity Framework Pre-Release Forum.

This walkthrough provides an introduction to the Productivity Improvement work that is included in our Entity Framework Feature CTP4 release. Our recent Design Blog post provides a detailed background on the Productivity Improvements, here is a quick summary extract from that post:

We’ve been paying attention to the most common patterns that we see developers using with the EF and have been brewing up a set of improvements to the Entity Framework designed to allow developers to accomplish the same tasks with less code and fewer concepts.

These improvements provide a cleaner and simpler API surface that focuses your attention on the most common scenarios but still allows you to drill down to more advanced functionality when it’s needed. We hope you will enjoy this simpler experience, but we should be quick to assure you that this is NOT a new data access technology. These improvements are built on the same technology for mapping, LINQ, providers and every other part of the Entity Framework.  

Database First & Model First

The Productivity Improvement work benefits developers using Database First, Model First and Code First development patterns. This walkthrough will use the Code First development pattern. The T4 Templates to support Database First and Model First development didn’t make it into CTP4 but we will provide some sample templates in a separate post in the coming weeks.

1. Install EF CTP4

If you haven’t already done so then you need to install Entity Framework Feature CTP4.

2. Create the Application

To keep things simple we’re going to build up a basic console application that uses the EF Productivity Improvements to perform data access.

· Open Visual Studio 2010

· File -> New -> Project…

· Select “Windows” from the left menu and “Console Application”

· Enter “EF.PI.Walkthrough” as the name

· Select “OK”

3. Create the Model

Let’s define a very simple model using classes. I’m just defining them in the Program.cs file but in a real world application you would split your classes out into separate files and probably a separate project.

· Below the Program class definition in Program.cs I am defining the following two classes

public class Category

{

    public string CategoryId { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Product> Products { get; set; }

}

public class Product

{

    public int ProductId { get; set; }

    public string Name { get; set; }

    public string CategoryId { get; set; }

    public virtual Category Category { get; set; }

}

 

4. Create a Context

The simplest way to start using the classes for data access is to define a context that derives from System.Data.Entity.DbContext and exposes a typed DbSet<TEntity> for each class in my model.

· We’re now starting to use types from the CTP so we need to add a reference to the CTP assembly

 o Project -> Add Reference…

 o Select the “.NET” tab

 o Select “Microsoft.Data.Entity.Ctp” from the list

 o Click “OK”

 

· You’ll also need a reference to the existing Entity Framework assembly

 o Project -> Add Reference…

 o Select the “.NET” tab

 o Select “System.Data.Entity” from the list

 o Click “OK”

 

· Add a using statement for System.Data.Entity at the top of Program.cs

using System.Data.Entity;

· Add a derived context below the existing classes that we’ve defined

public class ProductCatalog : DbContext

{

    public DbSet<Category> Categories { get; set; }

    public DbSet<Product> Products { get; set; }

}

 

That is all the code we need to write to start storing and retrieving data. Obviously there is quite a bit going on behind the scenes and we’ll take a look at that in a moment but first let’s see it in action.

5. Access Data

· I’m padding out the Main method in my program class as follows

class Program

{

    static void Main(string[] args)

    {

        using (var context = new ProductCatalog())

        {

            var food = new Category { CategoryId = "FOOD", Name = "Foods" };

            context.Categories.Add(food);

            int recordsAffected = context.SaveChanges();

            Console.WriteLine(

                "Saved {0} entities to the database, press any key to exit.",

                recordsAffected);

       Console.ReadKey();

        }

    }

}

You can now run the application and see that the new category is inserted.

Where’s My Data?

DbContext has created a database for you on localhost\SQLEXPRESS. The database is named after the fully qualified name of your derived context, in our case that is “EF.PI.Walkthrough.ProductCatalog”. We’ll look at ways to change this later in the walkthrough.

Model Discovery

DbContext worked out what classes to include in the model by looking at the DbSet properties that we defined. It then uses the default Code First conventions to find primary keys, foreign keys etc. The conventions are discussed in detail in this Conventions Design Blog post, the conventions included in CTP4 are:

  • Primary Key
  • Relationship Inverse
  • Foreign Key
  • Pluralization of Table Names

 

Simplified API Surface

If you’re familiar with ObjectContext you’ll notice that intellisense on your derived context now only shows you 10 members, as opposed to the 47+ that showed up on ObjectContext.

You’ll see we have started to factor functionality away so that only the most commonly used operations are exposed at the root level. For example all members related to the underlying database are available from context. Database. This is just one example of the work we are doing to provide a more discoverable and intuitive API surface, more detail is available in the API Surface section of our Productivity Improvements Design Blog post.

6. Access More Data

Let’s pad out the program we just wrote to show a bit more functionality. We are going to make use of the Find method on DbSet that will locate an entity based on primary key. If no match is found then Find will return null. We’re also making use of LINQ to query for all products in the Food category ordered alphabetically by name. Querying uses the exiting LINQ to Entities provider so it supports the same queries that are possible with ObjectSet/ObjectQuery in EF4.

· I’m replacing the Main we wrote above with the following

class Program

{

    static void Main(string[] args)

    {

        using (var context = new ProductCatalog())

        {

            // Use Find to locate the Food category

            var food = context.Categories.Find("FOOD");

            if (food == null)

            {

                food = new Category { CategoryId = "FOOD", Name = "Foods" };

                context.Categories.Add(food);

            }

            // Create a new Food product

            Console.Write("Please enter a name for a new food: ");

            var productName = Console.ReadLine();

            var product = new Product { Name = productName, Category = food };

            context.Products.Add(product);

            int recordsAffected = context.SaveChanges();

            Console.WriteLine(

                "Saved {0} entities to the database.",

                recordsAffected);

            // Query for all Food products using LINQ

            var allFoods = from p in context.Products

                            where p.CategoryId == "FOOD"

                            orderby p.Name

                            select p;

            Console.WriteLine("All foods in database:");

            foreach (var item in allFoods)

            {

                Console.WriteLine(" - {0}", item.Name);

            }

            Console.WriteLine("Press any key to exit.");

            Console.ReadKey();

        }

    }

}

7. Changing the Database Name

If you want to change the name of the database that is created for you then there is a constructor on DbContext that allows you to specify the name.

· Say we want to change the name of the database to “MyProductCatalog” we could add a default constructor to our derived context that passes this name down to DbContext:

public class ProductCatalog : DbContext

{

    public ProductCatalog()

       : base("MyProductCatalog")

    { }

    public DbSet<Category> Categories { get; set; }

    public DbSet<Product> Products { get; set; }

}

Other Ways to Change the Database

There are a number of other ways to specify which database should be connected to. We’ll cover these in more detail in a separate post in the near future.

  • App.config Connection String
    Create a connection string in the App.Config file with the same name as your context.
  • DbConnection
    There is a constructor on DbContext that accepts a DbConnection.
  • Replace the Default Convention
    The convention used to locate a database based on the context name is an AppDomain wide setting that you can change via the static property System.Data.Entity.Infrastructure.Database.DefaultConnectionFactory.

8. Setting an Initialization Strategy

In the next section we are going to start changing our model which in turn means the database schema needs to change as well. Currently there is no out of the box solution to migrate your existing schema in place, although this is something we are looking at addressing. There is however the opportunity to run some custom logic to initialize the database the first time a context is used in an AppDomain. This is handy if you want to insert seed data for test runs but it’s also useful to re-create the database if the model has changed. In CTP4 we include a couple of strategies you can plug in but you can also write custom ones.

For the walkthrough we just want to drop and re-create the database whenever the model has changed.

· At the top of the Main method in my Program class I’ve added the following code

Database.SetInitializer<ProductCatalog>(new RecreateDatabaseIfModelChanges<ProductCatalog>());

We’ll provide more details on this feature and the scenarios that it enables in a separate post.

9. Data Annotations

So far we’ve just let EF discover the model using its default conventions but there are going to be times when our classes don’t follow the conventions and we need to be able to perform further configuration. There are two options for this; we’ll look at Data Annotations in this section and then the Code First Fluent API in the next section.

· Let’s add a supplier class to our model

public class Supplier

{

    public string SupplierCode { get; set; }

    public string Name { get; set; }

}

· And we also need to add a set to our derived context

public class ProductCatalog : DbContext

{

    public ProductCatalog()

        : base("MyProductCatalog")

    { }

    public DbSet<Category> Categories { get; set; }

    public DbSet<Product> Products { get; set; }

    public DbSet<Supplier> Suppliers { get; set; }

}

 

Now if we ran our application we’d get an InvalidOperationException saying “Unable to infer a key for entity type 'EF.PI.Walkthrough.Supplier'.” because EF has no way of knowing that SupplierCode should be the primary key for Supplier.

· We’re going to use Data Annotations now so we need to add a reference

 o Project -> Add Reference…

 o Select the “.NET” tab

 o Select “System.ComponentModel.DataAnnotations” from the list

 o Click “OK

 

· Add a using statement at the top of Program.cs

using System.ComponentModel.DataAnnotations;

 

· Now we can annotate the SupplierCode property to identify that it is the primary key:

public class Supplier

{

    [Key]

    public string SupplierCode { get; set; }

    public string Name { get; set; }

}

 

Data Annotations are described in detail in this Design Blog post, the annotations supported in CTP4 are:

  • Key
  • StringLength
  • ConcurrencyCheck
  • Required
  • Timestamp
  • DataMember
  • RelatedTo
  • MaxLength
  • StoreGenerated

10. Fluent API

We looked at configuring the model using Data Annotations but you may not want to add attributes to your classes or in some cases you may not be able to if they belong to another class library. The other option is to use the Code First Fluent API within DbContext.

Let’s say we want to configure Name to be a required property for Supplier.

· Add a using statement for System.Data.Entity.ModelConfiguration at the top of Program.cs

using System.Data.Entity.ModelConfiguration;

 

· Override the OnModelCreating method in the derived context, as highlighted below
Note: The base implementation of OnModelCreating is blank so there is no need to call the base method.

public class ProductCatalog : DbContext

{

    public ProductCatalog()

        : base("MyProductCatalog")

    { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)

    {

        modelBuilder.Entity<Supplier>().Property(s => s.Name).IsRequired();

    }

    public DbSet<Category> Categories { get; set; }

    public DbSet<Product> Products { get; set; }

    public DbSet<Supplier> Suppliers { get; set; }

}

Summary

In this walkthrough we looked at the core patterns for Code First development using the EF Productivity Improvements included in EF Feature CTP4. We looked at defining and configuring a model, storing and retrieving data, configuring the database connection and updating the database schema as our model evolved.

We’d like to hear any feedback you have on these new Productivity Improvements for EF.

Rowan Miller
Program Manager
ADO.NET Entity Framework

 

EF.PI.Walkthrough.zip