Friday, May 22, 2015

Building a Blog - Pt2. Building a Database, Models and Entity Framework

I had some killing headache last few days, not sure is it caffeine or too much Coke or what is it, also there was an official event for getting my diploma but back in business! Now... To execute an... Pt.2 of Buliding Database for our Blog project using Code First approach. It will be quite lengthy as code will take a lot of place, so flush ice cold water into your eyes, make sure you avoid the rest of the face and let's go!

I want to use Code first approach as, I believe, it can bring a bit more understanding to what is going on when working with Models in our database in MVC. So let's create our models.

Since I said in the beginning of this blog that we are thinking for now only about Blog name and Posts. Let's create a Model for our Blog name.

Now you might wonder "why Model for a Blog name?" and... It's more about having a Model for multiple Blogs. Because you don't necessarily want to have only one Blog about anything. Some people have multiple blogs, if you are using Google's Blogger, you can see that you can make few Blogs not just one. So in that way we need a Model for our Blog. Since Models are presenting database, that is our purpose for the Blog Model. To have a table in a database that will remember the Names of the Blogs we want to have. I just say this so you can see the bigger picture, I am still going to stick to one blog for now. But it is good practice never the less to do so.

In Visual Studio 2013, on the project you have created, in the Solution Explorer, and then Models folder, right-click and Add a new Class. Call it a Blog. You should have now something like this:


============================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogProject_Dusan_MVC.Models
{
    public class Blog
    {

    }
}

=============================================


If so that's good. So the next question is, what our Blog model needs to have? An identification number which is also a primary key for our database table, and a Name. When I say what our Blog model needs to have it is the Database Table I think of. So we will add these properties:


=============================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogProject_Dusan_MVC.Models
{
    public class Blog
    {
        public int BlogID { get; set; }
        public string Name { get; set; }

    }
}


=============================================

That is all I want for Blog Model to have. Because what I am thinking now is what my next Model or Database Table need to have and that is Post model. I need to know that, when I create a Post as you would do in any Blog, I need to know to which Blog it is related to. So first create a new Class in Model folder in Visual Studio and call it Post:


=============================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogProject_Dusan_MVC.Models
{
    public class Post
    {


    }
}


=============================================

Same as Blog class just it is called the Post. Thinking of what the Model or Database table should have we have, again, ID that will make Primary key in our table in the database, then Title and Content. But we also have a Foreign key or virtual property that tells to this model, that one property is related to the other Model's property:

=============================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace BlogProject_Dusan_MVC.Models
{
    public class Post
    {
        public int PostID { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public int BlogID { get; set; }

        public virtual Blog Blog { get; set; }

    }
}

=============================================


Now you might wonder "how does this tell to what or whom, that this virtual property, and god knows what virtual is, is related to the Blog model?!". This is where the Entity Framework comes in. And what you need to know for "virtual" in Model, is that, it is being used to say to Entity Framework, that property BlogID has a navigation property, which in this case is virtual Blog property.


So the question arises "where do I get this Entity Framework thing!?" you install it as additional package to your Project. To do so go: Tools > NuGet Package Manager > Package Manager Console

Once you have Package Console up, type in:

Install-Package EntityFramework

You should get something like this:

Installing Entity Framework in Package Manager Console

So Entity Framework is a package that you install for your project in order to create Databases. Whether that is Code First, using Entity Framework Designer. You can also use it if you have an already existing database. You can read more about it here - Entity Framework

All right. We now have two models. Blog and Post and your Models folder in your Project should look like this:

Class files that represent Models in the Model Folder of Visual Studio Project

But we just have those two models that just stand there, one of them has something that says something to some guy called Entity Framework, but unfortunately, nothing would happen if we just simply start the application. Because our Database is yet to be created. However, we have to say to Entity Framework to create Database as well. It won't create a database on its own.

But to not make too much info at once and not too much reading at the same time, I will have to write that in another post. It will be probably the final part of creating the database for our Small and Simple Blog.