You are currently reviewing an older revision of this page.
Go to current version


This wiki is a transcript of a previously recorded video.

Related content assets:


Using ADO.net Entity Framework  Code-First Functionality

Hi, I’m Greg Low. I’m a SQL Server MVP and part of the Microsoft Regional Director Program. In this demo, I want to spend a few minutes showing you the code-first functionality as part of the ADO.NET Entity Framework Version 4.3.

We’ll start by opening Visual Studio, and instead of starting by building a database, and then reverse-engineering a data model from that database, there’s often a desire to do that in code first. And so this is the example here. I’ll start building a new project. I’ll take a Visual C# Web ASP.NET MV2 Web Application. Let’s call this CarpoolManager, create a directory, and okay. Now, with this project, it’s suggested you might want to create a Unit Test project. Most of the time I would do that, but in this case I don’t actually want to do that, because I’ll just avoid the complexity for now.

This will create my initial project. Now what I then do is start adding the classes immediately. In the models area here, I can say, look, add a class. I’ll call this CarTrip.cs, and add. Rather than having you sit there and watch me type, let me just grab an existing class and drop that into the code. So what I’ve got here is the CarTrip class. It’s got a TripID, a TripDate, a PassengerLimit, and then a collection of bookings. I’ve then defined the Booking class, where I’ve got a BookingID, a CarTrip, a PassengerName and a Trip. But notice I’m doing all of this without any reference to the database. Now I do need to make a few other references. At the top, grab the code – I need to make an entry for using System.Data.Entity. Let’s pop that in. And then, down the bottom of this, I need to make a reference to a class that’ll provide the database context. And so in this case, I’ve called that CarPoolBookings. We’ll pop this in.

Now notice what this is. It’s a db set of car trips, a db set of bookings, and the class that I’ve created called CarPoolBookings is inherited from db context. This is the thing that’ll later give us the reference to the database. Now, the final thing I need to do to make this an application, on the HomeControllers area, I need to put a reference to that class that I’ve just built. And so let’s grab the code for that. So I’d say, look, using the CarPoolManager models, and drag that in. And then I need to start replacing in an MVC application, some of the controller code.

So first up, instead of this existing index code, what I’ll do here is drop in a new version of the index. All I’m doing here is making an example where when somebody hits the index action in the controller, what I’m going to do is list any of the car trips where the PassengerLimit is still bigger than the number of people booked on it. And again, rather than having an About entry down at the bottom, I’ll just give a new Create option so that it’s possible to create these new objects. I’ll use that to replace the About.

Now the final thing about this is that we need to add some sort of View over the top of this. And so, if I took the Create action and said, look, I want to add a View, I can say the View name is Create. I’m going to create a strongly-typed View. What I’d have to have in here is a reference to the CarpoolManager models. And so this would be strongly-typed CarpoolManager models. CarTrip. And where this is going in place. And the type of thing we’re building is a Create. And then, once I add this in, this is going to give me my – I’ve got the initial page here – what I would need to do is sort of also pop in and sort of build an HTML page that we could use for doing this. Now in this case, I’ve got an example of what some of that code could look like, and in this case maybe just a MasterPage. Couple of content holders for this and the CarTrips, and then finally, an action that says Create a New Car Trip. And look, this is all I’d have to build to get my basic application up and going. And then once I start the app, this will go off and create a database including the tables, the foreign key relationships between the tables, and so on. And then all of that would be done, and built straight up without me building the database first up, based upon the classes that I have created inside here.

So this gets you started with working with just code-first functionality instead of database first.

Return to SQL Server 2012 Developer Training Kit BOM (en-US)

Revert to this revision