In this video, we'll take a look at another
kind of development workflow with Entity Framework, called Database First.
In which you have an existing database that you
want to work with and develop a new application around.
To demo this workflow, we'll create a new MVC application.
So, choose ASP.NET web application.
I'm going to call it DatabaseFirst.
Click OK, and then choose MVC from the templates.
Once it's ready, I'll open the server explorer with Ctrl+Alt+s.
If you forget the keyboard shortcut, you can also
find this under View >Server Explorer from the main menu.
And your backup plan if you forget that is the Quick launch
bar which will also remind you what the two other methods are.
Now, if we right click Data Connections and choose Add Connection,
I'm going to want to specify my instance of SQL server express local DB.
And the way I do that is with local DB in parens, followed by a slash V 11.0.
And then under Connect to a Database, I'm going to choose Attach a Database File.
And if you have access to the exercise files, you can find this in chapter three.
It's ExploreCalifornia.mdf.
Otherwise, you can use the database that you generated
if you were following along with the last video.
The logical name here is like an alias and not really necessary for this example.
So, I'm going to click OK.
Then expand the database.
Expand tables and if you right click the Tours table and do Show Table Data, you
can see that I have added more Tours
here to make our example little more interesting.
Another thing I have added is a related table called ratings.
Let's take a look at that one.
This stores the difficulty rating types.
So, instead of storing easy, medium, and difficult as strings in each
row of my Tours table, I'm now just storing a rating ID there.
This cuts down on storage space and also helps to normalize the data base.
So, in the DatabaseFirst approach, what I'll want to
do next is add what's called an entity data model.
I'll click on the Models folder, go to Add, and then I
can go right to ADO.NET Entity Data Model in this context menu.
And let's call it ExploreCalifornia.
We'll choose Generate from database.
And basically what we're going to be doing here is requesting that
an ORM, an Object Relational Mapper, be created for our application.
To be consistent with our last example,
let's change the connection setting's name to MyDbContext.
That name will actually be used for the DbContext class that's generated, as well
as a connection string that will be stored in the web doc config file.
Here we're being asked if we want to add
the data file to the project, and that's fine.
The only objects we're concerned with are the Tables.
And I leave the rest of the default settings.
The ones about pluralizing and singularizing object names,
that will have the effect of singularizing the
names of individual identities like tour and pluralizing
the names of the DB sets like tours.
Now, I see a designer view of my entity data model.
Which I can alter if I wanted to.
I could also start at a point like this and then generate a new database.
And that's what you'd do if you were doing what's called model first development.
If I click on the connector here you can see that it's well
aware that the rating ID of Tour is associated with the ID of rating.
Since there was a foreign key on rating ID in the database.
Now we can actually go ahead and create a controller and some views.
But before we do that, we're actually going to need to perform
a build and compile all of this code that we've just generated.
So far in this course, whenever we viewed something in
the browser, Visual Studio would
automatically compile our code if necessary.
But to add controllers and views that are auto-generated for us, we need
to make sure all of our compiled libraries are up to date first.
So, we can right click the project or the solution and
choose Build and as long as that's succeeds, we can go
ahead and add a controller now, and you can see in
the output window below, that one project succeeded with this build.
So, now we can right click controllers.
And add the same kind of controller that we added in
the last video, an MVC5 controller with views using Entity Framework.
Call it TourController, again select Tour for the
model class and MyDbContacts from the data contacts class.
And click Add.
Now let's go to the create view and view it in the browser.
So notice, I have some default labels now which I'd want to adjust a little bit.
But one nice thing that is set up for me automatically is that rating ID drop down.
Because the scaffolding engine is actually smart enough to know that
I want to display the name field there from the related table.
If I wanted to change that, I can find that logic in the controller.
So, you can see in the create action, it's
adding a dynamic property to the ViewBag called RatingId.
This is a list of name value pairs generated from the
ratings dbset in the ID and name properties of those ratings.
And then the view is going to be able to use that list to create the drop down.
We also have all of the pages for the other operations that we've seen before.
We don't have the branding and the styling
that we had with the last example but we've
come a long way in just a few
minutes starting only with a couple data base tables.
On this index page, notice that the heading on the rating column says Name.
That's because it comes from the name property of the rating class.
To fix that, remember we can just edit the rating model, and in this Database First
project, that class is going to be found under the entity data model file here.
And then, underneath explorecalifornia.tt, which is a text
template that was used to generate these classes.
And the structure of the rating class looks a little different than what
we created ourselves for the tour class when we were doing code first.
This class will actually contain a reference to the related tours.
But we can do the same thing that we did before, just add a display attribute after referencing
System.ComponentModel.DataAnnotations.
And remember, we wanted to change name to something more meaningful, like rating.
Let's go back to the index page.
Remember Ctrl+Shift+W will auto-save and compile.
And that clears up this column naming problem for us.
So, that should give you a sense of what it's like to develop and
application where the database is the foundation
using the Database First approach with Entity Framework.
In the next video, we'll talk about how
to write your own custom queries using linked entities.