Find out how to create the context class that will tie the database to the existing MVC application.
- [Instructor] With everything in place, we're ready to do a little bit of code first database modeling and we'll start by creating our model class. Let's come over to models, right click and say Add, Class. We're going to create a simple class called order. In our order class, for now we're going to have just three public properties, the order ID, the name and details, which will be a collection of detail objects.
Let's start by creating the detail objects. So, we'll go to models, right click, Add, Class and this one we're going to call Detail. Our detail class is slightly more complicated. It's going to have a detail ID and a description and then it's going to have its foreign keys for order and order ID. Okay, in the detail class, we're going to create a few public properties, we're going to have our key detail ID, the description, and two foreign keys, one for order ID and one for order.
To get started we'll create our detail ID, so we will create the attribute key, and notice the red squiggly line. Let's get Visual Studio to help with that by adding the appropriate using statement. With that in place, we're ready to say public, int, DetailID, and we'll use automatic getters and setters. Next, we want to have our description, which will be a string.
And we'll give that a get and a setter as well. And now we're ready for our foreign keys. The first one will take the attribute foreign Key and we're going to pass in the foreign key, which will be order. Let's see if Visual Studio can help with our red squiggly line again. We need another using statement. Excellent. And that is going to be public int OrderID.
And then finally we're going to have an attribute Foreign Key of OrderID, and that will be a public order object, which we have not yet filled in. We could create the order object, and that will be Order, get and set.
So, that is our detail object, we can now return to our order object and in the order class, we can have a key once again and once again we will add the using statement we need. And we're going to have public int OrderID, which you'll remember we used in the detail class. Get and set.
Then we're going to say public string Name, get and set and we're now set up to create a collection of the detail objects we just defined. So, we'll say public virtual Icollection and we'll make that of Detail, and we'll name that Details.
And that is our order class. We now need to add our EntityFramework NuGet package, so we're going to go over to tools, NuGet Package Manager, and we might as well go to the Manage NuGet Packages for Solution and here we see the list of installed NuGet packages. We're going to go over to browse, and we will browse for EntityFramework.
And the first thing to come up is EntityFramework, which is just what we want. So we'll click on that and add it to our project and say install. We will take the latest version. It's going to ask us to review. We'll say okay. It's going to ask us to accept the license. We'll accept it and it will add EntityFramework and any dependencies it may have. Now it wants to know if web config should be reloaded, we'll say yes.
EntityFramework was added successfully. Let's go back to web config and double check that our login is still here, which it is. MyDBConnection and it has our user ID and password. So, that is still set properly. Next we want to create the context class and we want to do that inside a new folder called Services. So, let's come over to our project, right click on the project, and say Add, New Folder, and we're going to call that services.
And within services we're going to create the context class. So, we'll right click on services, create a class, call that class context. Now, the context class is actually a class derived from DBContext. So, let's add a couple using statements that I know we're going to need. We'll say using system data entity and we'll say using, and let's see.
What did we call this application? WebAppForData dot and we want to bring in the models. With those in place, and under the name space WebAppForData Services, we're ready to create our class, and we will call that class Context and we will have it derived from DBContext.
Inside the class we're going to have a constructor and a couple properties. So, let's create the constructor. And we're going to call up to the base and say in quotes that the name equals, and you remember the name of our connection was MyDbConnection. Let's double check that by going back to web config, and it is in fact MyDbConnection with all the appropriate capitalization.
We can go back where we were and we need to make sure we put in the braces for our constructor. Now the public properties. The first one we need is a Db set for our order table and a second Db set for our details table. So, we'll say DbSet and we need to say of Order and we'll call that Orders, give it accessors and then very similarly we'll say public DbSet of detail, which we will call Details.
That creates our context and the next thing that we're going to want to do is a migration, which we will do next.
- Creating an MVC web app
- Publishing an MVC app to Azure
- Creating a SQL database on Azure
- Enabling migrations
- Updating an Azure database
- Changing the schema
- Exploring various types of Blob storage
- Creating Blob storage accounts in Azure
- Examining Azure statistics for monitoring Blob storage