BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

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

This item in japanese

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

BT