Foreword

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.”

1.  Launch Visual Studio 2010.

2.  From the View menu, select Server Explorer.

3.  In Server Explorer, add a new connection to your EntityFrameworkIsSimple database.

4.  Create a new Console Application project, and call it EntityFrameworkIsSimple.

5.  Right-click the project and select Add > New Item. In the Add New Item dialog box, select ADO.NET Entity Data Model.

6.  Click Add.

7.  In the Entity Data Model Wizard that comes up, select Empty Model and click Finish.

8.  The entity designer will appear. Right-click in it and select Add > Entity.

9.  In the Add Entity dialog box, set the entity name to Person. This will automatically make the entity set People. (The set is the name of the collection to which you’ll add new instances of the Person class.)

10.  Click OK.

11.  A new entity will appear. Right-click on the Properties bar inside of it and select Add > Scalar Property. (Or just click on the Insert key.)

12.  Rename the new property to FirstName.

13.  Do this again, creating a new property called LastName.

14.  Add another entity and call it Book.

15.  To this new entity, add a property called Title.

16.  Right-click the “Person” text in the Person entity and select Add > Association.

17.  In the Add Association dialog box, change the Multiplicity on the Person end to * (Many), and change the Navigation Property value at right, from Person to Authors.

18.  Click OK.

19.  At this point, your model should look like this:

20.  Now, right-click on an empty area of the designer and select Generate Database from Model.

21.  In the Generate Database Wizard that comes up, provide a connection to your database. Because we’ve added a connection to the database at the beginning of this walkthrough, it should show up in the drop-down list of available connections.

22.  Click Next.

23.  The DDL for a database to hold your model shows up. Click Finish.

24.  In the T-SQL editor that comes up, right-click and select Execute SQL. Provide your local database information when asked to connect.

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:

1.  In the designer, right-click in an empty area of the canvas and select Properties.

2.  In the Properties window, find the property called Entity Container Name and change its value to SimpleModel.

3.  In Program.cs, enter the following code into the body of the 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();

4.  Compile and run this code. You should see the following output:

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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.31.22