[Entity Framework] Code First – Overview

What is EF Code First? Why are all developers talking about this? Entity Framework is quite easy to use and very powerful. So why more? Let’s find out.

 

1. Development of EF

When the first version of EF was introduced, it was a common practice to create DB first and map it to classes using ORM (Object-Relational Mapping) tools. The EF Designer and wizard show exactly how this works.

What if you do not have DB yet. It might be convenient to work with classes first and create DB later. That’s how the Model First approach was proposed. You can use the EF Designer to create classes without DB and later you can create DB from the existing model classes.

But both of these two options are dependent on the EF designer and the edmx file. Your model classes are still dependent on the EDMX file.

The next movement is to incorporate POCO (Plain Old CLR Object) with EF. You can use your model classes in conjunction with EF model classes. EF provides the template to create POCO classes from the EF model (edmx).

Code First is the next movement. The EF model (edmx) is completely removed. You do not use the EF designer and the wizard anymore.

 

2. Database First

  • Create the Database
  • Create an edmx file using the EF wizard
  • Model classes are auto-generated from the edmx file

 

3. Model First

  • Create an edmx file using the EF wizard
  • Model classes are auto-generated from the edmx file
  • Generate DB from the edmx file

 

4. Code First

You do not create an edmx file anymore. Rather you just stick to POCO classes.

Without an edmx file, there should be a way to specify how the model classes and DB are mapped. Mostly the mappings are determined by conventions (or default configuration values). You can modify this default behavior using the Data Annotations (attributes) or the Fluent API.

  • Create POCO classes
  • Configure POCO classes using data annotations or the Fluent API
  • Create a context object using “System.Data.Entity.DbContext
  • Add model classes to the context using “System.Data.Entity.DbSet

In Code First, your domain classes become the data model classes on which EF relies. IMHO, it is the best part of Code First.

 

5. Model Classes

Your model classes are not related with EF anymore. “System.Data.Objects.DataClasses.EntityObject” is a base class for entity types that are generated by the Entity Data Model tools.

public class Category
{
  public int Id { get; set; }
  public string Name { get; set; }
  public List<Product> Products { get; set; }
}
public class Product
{
  public int Id { get; set; }
  public string Name { get; set; }
  public Category Category { get; set; }
}

If these classes are created by edmx, they look like this:

public partial class Category : EntityObject
{
  ...
}
public partial class Product : EntityObject
{
  ...
}

 

6. DbContext

In EF, “System.Data.Objects.ObjectContext” was used as a context object. In Code First, “System.Data.Entity.DbContext” is a main context object.

Also, “System.Data.Entity.DbSet<TEntity> is used for entity types. (“System.Data.Objects.ObjectSet<TEntity>” is used for EF.)

public class ProductContext : DbContext
{
  public DbSet<Category> Categories { get; set; }
  public DbSet<Product> Products { get; set; }
}

If the context is created by a designer, it will look like this:

public partial class ProductContext : ObjectContext
{
  public ObjectSet<Category> Categories { ... }
  public ObjectSet<Product> Products { ...}
  ...
  public void AddToCategories(Category category)
  {
    base.AddObject("Categories", category);
  }
  public void AddToProducts(Product product)
  {
    base.AddObject("Products", product);
  }
}

 

7. Creating DB

When you create and reference theĀ DbContext object, the runtime checks whether the DB exists and create one for you if it does not exist.

using (var context = new ProductContext())
{
  var query = from c in context.Products select c;
  foreach (Product p in query)
  {
    Console.WriteLine("{0}: {1}", p.Id, p.Name);
  }
}

By default, local SQL Server Express instance is used and the fully qualified class name of the “DbContext” object is used as the name of a new database.

You can find the following database is created for you automatically.

It creates primary keys and even figures out foreign key relationships.

 

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 )

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