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 realworld
situations.

No comments:
Post a Comment