In previous posts we've explored creating a database-first model and creating a model-first model. Both of those designs used the same thing: an EDMX file. In order to use that file, we had to use a designer. But what if we don't want to bother with a designer? Maybe we'd rather just work with code.

Well, good, because that's what we're going to do in this post. We're going to return to the Northwind database and use it to create a code-first schema.

Code-First is the design philosophy in EF that allows developers to work with simple C#/VB classes rather than an EDMX file. For those of us who think in classes rather than designers, this comes in handy.

As a reminder, here's what the full Northwind database schema looked like:

Also, you will need Entity Framework version 6.1 at a minimum to create a Code-First model from an existing database.

Enough talk. Show me the code!

First, right click on the folder where you want your model to go, select Add -> New Item, then select ADO.NET Entity Data Model.

Click Add, and select "Code First from Database", then click Next.

On this next screen, pick your connection information and name the connection string, then click Next.

On this last screen, you can now pick which entities you want to include in your model:

IMPORTANT: Note that Code-First does not currently support adding Stored Procedures as entities. You will instead need to call the sproc manually, as shown here

To build your model, click Finish. The model generated should look something like this:

Notice that there's no EDMX file, only CS files. This is, of course, the definition of Code-First: you only work with code files.

Let's take a loot at a few of these files. First, Customer.cs:

public partial class Customer
    public Customer()
        Orders = new HashSet<Order>();
        CustomerDemographics = new HashSet<CustomerDemographic>();

    public string CustomerID { get; set; }

    public string CompanyName { get; set; }


    public string Phone { get; set; }

    public string Fax { get; set; }

    public virtual ICollection<Order> Orders { get; set; }

    public virtual ICollection<CustomerDemographic> CustomerDemographics { get; set; }

A few things to notice about this class:

  • The Customer class has two collection properties: Orders and CustomerDemographics. These are the related entities for this class (Navigation Properties).
  • Notice all the StringLength attributes? They're being used to correspond with the varchar(X) values in the database.
  • Also, see that Required attribute? That means that CompanyName cannot be null.

You'll notice by looking at the source on GitHub that there is a class for each entity, as well as an additional class called Northwind.cs. That class is the context, which is the class that encapsulates all of the other classes (just like the EDMX did). Let's take a look at it.

public partial class Northwind : DbContext
    public Northwind()
        : base("name=NorthwindCodeFirst")

    public virtual DbSet<Category> Categories { get; set; }
    public virtual DbSet<CustomerDemographic> CustomerDemographics { get; set; }
    public virtual DbSet<Customer> Customers { get; set; }
    public virtual DbSet<Supplier> Suppliers { get; set; }
    public virtual DbSet<Territory> Territories { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)

First, look at the constructor. It is passing the name of the connection string into a base constructor for the DbContext class.

Next, we see the class DbSet. DbSet represents the collection of all entities that can be queried from the database.

The most interesting part of all this is that these classes that EF generated for us are the same classes generated by the EDMX's code generation tools. The only thing that Code-First is really doing differently is allowing us direct access to these generated files, removing the designer.

Now you've got your model created! There's still one more part to this series, and that's creating a code-first model from scratch. We'll do that in the next installment of Entity Framework for Beginners.

Happy Coding!

Hello Dear Reader! Want to get the best C#, ASP.NET, web tech, tips, and stories anywhere on the Web? Sign up to receive my premium newsletter The Catch Block! Each Wednesday, you'll get the best reads, job listings, stories, tips, and news from around the ASP.NET and C# worlds. All for only $5/month or $50/year! Become a subscriber today!