BT

InfoQ Homepage News EF Feature CTP 4 Brings Code First Updates and Productivity Improvements

EF Feature CTP 4 Brings Code First Updates and Productivity Improvements

Bookmarks

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.

Rate this Article

Adoption
Style

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.

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

Community comments

  • Entity Framework 4.0 is already RTM

    by Diego Vega /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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 /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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

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

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.