A few days ago, Microsoft released CTP4 of the Entity Framework Feature. In his blog post demonstrating the new features of the EF CTP4, Scott Hanselman proposed a better name than the official “Microsoft ADO.NET Entity Framework Feature 4″: “EF Magic Unicorn Edition”. I quite like that name, so I’ll use it from now on.
The biggest addition the Entity Framework is the ability to use a code-first approach. This means that you just write your basic (POCO) classes representing your model and let the EF Magic Unicorn take care of all the rest. No fiddling around with designers or XML bindings. Instead you can just concentrate on your code!
I thought this looked rather cool, so I decided to give it a go and it only took me a few minutes to get a working example.
Creating a new ASP.NET MVC 2 project
Let’s start with creating a new blank ASP.NET MVC 2 project. Because I want to see how EF Magic Unicorn handles many-to-many associations and self-referencing associations, we’ll to use something very common: posts and categories. A post can belong to multiple categories and a categories can also have multiple posts. A category can also have a parent category and multiple child categories.
Adding the model classes
Now that we have our blank project and we know what we want to test, let’s add two classes to the Models folder:
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public DateTime PublishDate { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public Post()
{
Categories = new List<Category>();
}
}
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual ICollection<Category> ChildCategories { get; set; }
public Category()
{
ChildCategories = new Lis<Category>();
}
}
Adding a reference to the Magic Unicorn
We need to download the EF Magic Unicorn. You can grab the installer here. When you run the executable, you can specify where you want to install the Magic Unicorn. Remember that location.
When the installation is complete, choose to add a reference to your ASP.NET MVC 2 project, browse to the Magic Unicorn installation directory and select the Microsoft.Data.Entity.CTP.dll file.
Creating the DbContext
Now that we have our classes in place, we somehow need to tell the Magic Unicorn about them. This also turns out to be very easy indeed. All we need to do is add a class that inherits from the DbContext class (this is new in EF Magic Unicorn) and add a generic DbSet collection (also new in EF Magic Unicorn) for each model class.
using System.Data.Entity;
public class MagicUnicornContext : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Category> Categories { get; set; }
}
Adding a connectionstring
At this point, we still haven’t created a database. And we won’t need to! We just have to tell the trusty Magic Unicorn where it can create one for us. To do that, we only have to add a connectionstring to the Web.Config with the same name as our DbContext: MagicUnicornContext.
<add name="MagicUnicornContext"
connectionString="Data Source=AITWS13\sqlexpress;Initial Catalog=MagicUnicorn;Integrated Security=True;Pooling=False"
providerName="System.Data.SqlClient" />
Adding a controller to test everything
Now we can add a controller to see if everything works. Just add a controller called HomeController, and inside the Index() action method, you can add the following code:
public ActionResult Index()
{
// Create a new Category
var parentCategory = new Models.Category
{
Name = "Parent category"
};
// Create a new Category and set the first one as its parent
var firstChildCategory = new Models.Category
{
Name = "Child category 1",
ParentCategory = parentCategory
};
// Create a new Category and set the first one as its parent
var secondChildCategory = new Models.Category
{
Name = "Child category 2",
ParentCategory = parentCategory
};
// Add the second and third Category to the ChildCategories of the first Category
parentCategory.ChildCategories.Add(firstChildCategory);
parentCategory.ChildCategories.Add(secondChildCategory);
// Create a new Post
var post = new Models.Post
{
Title = "New post",
Text = "The contents of the first post",
PublishDate = DateTime.Now,
};
// Add the third Category to the list of Categories of the Post
post.Categories.Add(secondChildCategory);
// Create an instance of our context
Models.MagicUnicornContext muc = new Models.MagicUnicornContext();
// Add the Post and the Categories
muc.Posts.Add(post);
muc.Categories.Add(parentCategory);
muc.Categories.Add(firstChildCategory);
muc.Categories.Add(secondChildCategory);
// Save everything
muc.SaveChanges();
return View();
}
What we do here is create a new post and three new categories. The first category becomes the parent of the other two categories. The second child category also gets added to list of categories of the post. Next we create a new instance of our MagicUnicornContext, we add the post and the categories to the context and call SaveChanges() on it.
Running it
After giving everything a final check and adding a default view, you can hit Ctrl-F5 and if all goes well, a blank browser window will apprear. It should be empty because our default view is still empty. So that means that no errors have occured!
Check the database
Now we only have to see if the Magic Unicorn actually created our database. You can fire up SQL Server Management Studio, or use the Server Explorer in Visual Studio, connect to your SQL Server and check out the databases. There should be database called MagicUnicorn that wasn’t there before!

When you open the tables, you will see that the Post, the three Categories and the links between were added as well.
Further reading
For more detailed guides about the EF Magic Unicorn, you can have a look at these excellent articles: