Now, take a look at how we can automatically generate a
database with tables, using a simple C# class in Entity Framework.
Because we've already defined a class for
the information we want to store, we can use
what's known as the first code approach, to
create the database based on the existing class.
The first thing we need to create.
Is a class that inherits from DbContext.
That's going to expose entities in the database
using public properties of a type called DbSet.
So I'm going to continue where we last left off
with our explore California MVC application, in the Exercise Files.
Chapter 3, ExploreCalifornia, and open the ExploreCalifornia solution.
Next, I'm going to add a class to the Models folder.
So go to Add > New Item, and then under Code on the
left I'm just going to choose an empty C# class and call it MyDBContext.
First, we are going to inherit from the DbContext class.
And I don't have a reference to that.
I showed you right-click resolve before.
You can do also, Ctrl +.
Or Ctrl +.
And quickly add that using statement.
So now we've added a reference to System.Data.Entity.
And we can finish up this class.
I'm going to add a constructor.
And the constructor, we'll simply call the base class constructor.
The base class constructor takes a database
connection string or a name of a database.
Since neither of those exist, I can just put ExploreCalifornia
here and it's going to configure that connection dynamically for me.
And create a database called
ExploreCalifornia when I run my application.
And we'll also add one public property.
And it's going to be a generic DbSet that holds the Tours.
We'll call it Tours.
And when we eventually run the application again and
create a new tour, it'll be add to the
Tours DbSet and that will have the effect of
creating a new database which contains a Tours Table.
That is based on our tour class.
And it will do that using SQL server express local db.
Which we installed along with Visual Studio Express.
Now if we go to our TourController, we
can add a private instance of our DbContext class.
I'm just going to call it db
And the rest is a piece of cake.
In the Create action, which I can get to quickly from this drop-down.
The one that takes a tour, and is invoked for post requests.
All we need to do now is add this tour to the Tours DB set, with db.Tours.Add.
And then we'll call the SaveChanges methods on DB.
Now we'll go to our Create view
and open it up in the browser.
And I"m going to fill out this form once more for the Big Sur Retreat.
Now when I click Create.
Entity Framework is going to look for
an existing database using the ExploreCalifornia name.
And when it's unable to find one, it will just go ahead and create one for me
and also create the tourist table and insert
a row using the form values I've just submitted.
Remember this index page that we see now
is just something we created as a place holder.
Since the redirect was already in the code when we created the scaffolding.
And at this point, you're probably wondering how we can verify that
anything at all just happened, and we'll do that in two ways.
First lets take a look at the database.
By default, the database will be created in the app_data folder.
But it won't be automatically added to the project, so we can
only see it if we click this Show All Files button up here.
If I double-click the database file now it
will open it up in the Server Explorer window.
Expanding the tables folder shows that it did
create a tours table and if I expand that.
I see that it has all the columns I need.
And notice that it was smart enough to make my ID property a primary key.
And finally, if you right-click the Tours table you can
either do a new query or just show the table data.
And you can see that the new record was inserted.
So that's pretty cool how much happened behind the scenes just now.
Another way we could have verified that we'd successfully
inserted this data is to display it on a page.
I don't yet have a page that does that but now that I have my database context set
up, I can regenerate my controller with a complete
set of views for all of the basic create.
Read, update, and delete operations.
And if I right-click Controllers, and chose Add Controller.
This is an option that you may have noticed
when we were creating the simpler version of the controller.
MVC 5 Controller with views, using Entity Framework, then click Add.
I'm going to give it the same name as
before and let it overwrite the existing one.
Again, choose Tour for your model class and
MyDbContext for the data context files, and click Add.
And we're going to allow this.
So now let's take another look at TourController.
Make some space.
So now we see a little bit more logic in these flushed out action methods.
And if I go to the Index view, and open it up in the browser.
I can see the tour that I created.
And let's make sure we can create another one,
which we can do by clicking this link here.
This is going to be for the Channel Islands excursion.
That's just a Day trip hike.
And this one is Easy and no free meals.
And I can also do Edit, or Delete.
We've now seen a little bit of what Entity Framework can do
using little more than the simple model in DbContext classes that we created.
If you decide to use this code first approach for your development needs.
You'll be glad to know that
Entity Framework also features a convenient mechanism
for updating your database with any modal changes you might have to make.
This is known as code first migrations.
In the next video, we'll take a look at the reverse
process of what we've just seen, which is database first development.