I spend a lot of my time here at Microsoft thinking about complexity—and asking myself lots of questions. My guess is that you do the same.
When we design code, we ask ourselves questions such as these: Can I make this code more readable? Can I write this loop with fewer lines? Can I factor out behavior into a separate class? Can I architect this system so that it is more cohesive?
When we design user interfaces, we ask similar questions: Are we asking the user to make too many decisions? Did we lay out this UI in the clearest possible way? Can we make error states clearer and easier to avoid?
When we design systems, we ask other questions: How many concepts must the user learn? Do those concepts map to things the user knows and cares about? Does everything hang together in a clear, sensible, consistent way?
I think about these things a lot. But first I’d like to answer another question that I often get asked: Just how complicated is the Entity Framework? The answer is, that it depends on what you want to do with it.
To see how simple the Entity Framework is, let’s spend five minutes making it jump through a simple set of hoops. You’ll need Visual Studio 2010 (the Express editions will work) and SQL Server (again, the Express editions will work just fine). In SQL Server, create a database called “EntityFrameworkIsSimple.”
* (Many), and change the Navigation Property value at right, from Person to Authors.
That’s it! We’ve got a model. We’ve got code. We’ve got a database. We’ve even got a connection string in App.Config that the designer creates and maintains for you.
Let’s take this model for a test drive. Let’s name the model:
SimpleModel.Main function://Create and write our sample data
using (var context = new SimpleModel()) {
var person1 = new Person() { FirstName = "Stefano", LastName="Mostarda" };
var person2 = new Person() { FirstName = "Marco", LastName="De Sanctis" };
var person3 = new Person() { FirstName = "Daniele", LastName="Bochicchio" };
var book = new Book() { Title = "Microsoft Entity Framework In Action"};
book.Authors.Add(person1);
book.Authors.Add(person2);
book.Authors.Add(person3);
context.People.AddObject(person1);
context.People.AddObject(person2);
context.People.AddObject(person3);
context.Books.AddObject(book);
context.SaveChanges();
}
//Query our sample data
using (var context = new SimpleModel()) {
var book = context.Books.Include("Authors").First();
Console.Out.WriteLine("The authors '{0}' are:", book.Title);
foreach(Person author in book.Authors) {
Console.Out.WriteLine(" - {0} {1}", author.FirstName, author.LastName);
}
}
Console.Read();

As you can see, we’ve created a system that issues queries and updates three different tables. And not a single join statement in sight!
Of course, in the real world, we have many other concerns: How do we bind these types to UI elements? How do we send and update them across distributed application tiers? How do we handle concurrency, dynamic querying, and stored procedures? While the Entity Framework may be simple to get started with, the real world is not simple, and the Entity Framework has a host of features for dealing with real-world situations.
Including an example like this may not be standard for a foreword to a book, but I did so to show how easy getting started with Entity Framework is and also to show you where this book comes in. Entity Framework 4 in Action will take you from handling transactions to understanding how to deal with performance problems and using ESQL to writing dynamic queries. And it will answer all of your questions along the way—even ones you did not know you had!
I look forward to seeing what you will do with the Entity Framework and to hearing what you want us to work on next. The authors are as excited as I am to show you what is in store in the future!
Noam Ben-Ami
Program Manager
Entity Framework Team, Microsoft