BT

EF Feature CTP 4 Brings Code First Updates and Productivity Improvements

by Abel Avram on Jul 15, 2010 |

Entity Framework Feature CTP 4 recently released updates the Code First programming model with new code conventions and data annotations, and adds two new types, DbContext and DbSet<TEntity>, providing easy entity model setup using Code First.

Code First

The Code First programming model is a way of creating a model by writing the code for the classes whose associated entities are contained by the model. The model is discovered by EF when using appropriate code conventions, and it can be refined using a fluent API or data annotation attributes. An example of using data annotation attributes looks like this:

public class Person {
    [Key]
    public string SSN { get; set; }
    [StringLength(512)]
    public string Name { get; set; }
    [RelatedTo(RelatedProperty=”Author”)]
    public ICollection<Book> Books { get; set; }
}

The [Key] annotation specifies that a property is the entity’s primary key, while [StringLength(512)] specifies the maximum length of a string property. EF CTP 4 recognizes the following data annotations: Key, StringLength, ConcurrencyCheck, Required, Timestamp, DataMember, RelatedTo, MaxLength, StoreGenerated.

Fluent API takes precedence over data annotations which in turn have precedence over code conventions. Other significant Code First updates are:

  • Model discovery conventions supported in CTP 4 are: Primary Key, Relationship Inverse, Foreign Key, Pluralization of Table Names
  • The Fluent API methods have been refined to be more intuitive. The Code First Walkthrough contains more details.

Productivity Improvements

EF Feature CTP 4 comes with 2 new types: DbContext and DbSet<TEntity>. DbContext is a simplified version of ObjectContext, providing basic functionality for model caching, database provisioning, connection management, and schema creation. An example looks as following:

using System.Collections.Generic;

using System.Data.Entity;
namespace MyDataAccessDemo {
class Program {
static void Main(string[] args){
using (var context = new ProductContext()) {
var food = new Category { CategoryId = "FOOD" };
context.Categories.Add(food);
var cheese = new Product { Name = "Cheese" };
cheese.Category = context.Categories.Find("FOOD");
context.Products.Add(cheese);
context.SaveChanges();
}
}
}
 
public class ProductContext : DbContext {
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}

public class Product {
public int ProductId { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
}
 
public class Category{
public string CategoryId { get; set; }
public string Name { get; set; }
public ICollection<Product> Products { get; set; }
}
}

This code is all one needs to write in order to have an entity model created for him, including all underlying plumbing needed to store the model in a database.

Microsoft has not yet specified when EF Feature 4 RTM will be shipped, mentioning only that “we are working to get to an RTM in the earliest feasible ship vehicle.”

Related information: ADO.NET EF CTP 4, EF CTP4 Walkthrough: Code First, Conventions for Code First, Productivity Improvements for the Entity Framework, EF CTP4 Walkthrough: Productivity Improvements.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Entity Framework 4.0 is already RTM by Diego Vega

What the article describes is the Entity Framework Feature CTP 4, which is the fourth version of the EF Feature CTP, which contains previews of new features that are in development. EF4 RTM is part of .NET 4.0, which went RTM last April.

Re: Entity Framework 4.0 is already RTM by Abel Avram

Hi Diego, your observation is correct. I edited the article by adding "Feature" to avoid any confusion. Thanks.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

2 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT