Jun 20, 2011

MVC 3 : Create A Simple Blog Application Pt. 2



We will continue from what we left out on previous tutorial Pt 1.  Some recaps on previous tutorial, we have create a new project that we called NegativeZero on our Microsoft Visual Studio(or some might be using Microsoft Web Developer).  Since we do not yet touch any code on previous tutorial, so by default the project will be compiled as Default MVC 3 Web Application Internet.

So on this part 2 tutorial, we will be creating our Model for this project.  Since we are developing simple but working Blog Application, we should bear in mind that one must has entity in our model.  The entity is POST which will keep our record on every post that we will post.  And basically, Post entity should have this attributes:

  1. author
  2. date
  3. title
  4. post(which will be the post body)
  5. tags
  6. categories

So, as I mention on previous tutorial, we will be using LightSpeed as our ORM in this project.  Some might be arguing why must I use non-free plugin? The answer is simple, LightSpeed offer free working version but limit the models to 8 entity. The major loss for free version is we cannot use more than 8 entities per Class and we do not the source code.  Well, for me – it's not a major loss as I am not interested on the source and I could create another 8 models by creating another class.

To start creating a model, just right click on Models folder and select New Items. This then will bring up a window as a wizard to add new item according to your needs.  Well for this, we need to create a Data Models – So we choose Data and select LightSpeed as our Modeling ORM.  I'll name my class file as NegativeZero.cs (surely you could name your file as you like).

Above is a photo gallery on all the steps for part 2 tutorial. You can take a look while following this text instruction to have a clear view on this guide.  We'll proceed to the next step, once we have create our LightSpeed model, we will now create the entity by using toolbox located at left corner tabs.

It's easy to create entities with LightSpeed, all you need to do is drag and drop.  I think you get the idea – So I'll just give the entities and attributes of each entities for you to do.  Below is the list of entities that we need to create:

  1. Post
    • Title
    • Author
    • Post
    • Tag
    • Categories
  2. Comment
    • Author
    • Email
    • Comment
    • Url

Once you have create these entities, we will create the relationship between the two entities. To create entities between these two, we use the need to use the toolbox again and select the One to Many Relationship.  Drag the cursor from Post to Comment entity. Don't drag it in reverse order because we want the post to have many comments not the other way around.  But if you mistakenly drag it in the wrong order, we can simply delete and re-drag the relationship again.

Once we have settled with our relationship, we click CTRL+S to save the models diagram.  By default, LightSpeed will auto magically create your class based on your diagram.  It's simple isn't it? This feature also available with the built-in LINQ-to-SQL data modeling ORM.  Now we need to create the SQL string connection.  This could be tricky if you do not have any experience with the Server Explorer to connect to your installed SQL Server. 

Just follow the picture I've attached on the SkyDrive and it should be solved.  Once you have copy the connection string, you need to paste it on the LightSpeed properties(also included in pictures).  Once you have copied the connection string, we can now create our database by right-clicking on empty space on model diagram, and select the Update Database(please be aware not to choose Update from Database because this will empty our model diagram because our new database do not have any tables at the moment).  Once you have select the option, LightSpeed will automatically transform our model diagram into SQL query and create the tables for us.  So we do not have to be expert in SQL to create this one.  You could do modification on the diagram later and update the database by using the same method.

Once we have our database created, we open the Get Started window and follow instruction from there to finalize our connection from Database to our Application by editing the web.config file. 

Once done, we have successfully create our simple blog models that we can used for our next tutorial which will cover on creating forms to make a post.  Until my next tutorial, you could play around with current project to see whether our LogOn models is working or not by creating user.  This models created automatically when we create our project by using Internet Application templates on tutorial part 1.

Until then, please leave comments on any confusion, or question(s). And don't forget to Like this post if you think it is helpful.


Blogger Labels: MVC,Create,Simple,Blog,Application,tutorial,Some,NegativeZero,Microsoft,Visual,Studio,Developer,code,Default,Internet,Model,POST,author,version,Class,folder,Items,item,needs,Data,VIEW,SLIDE,DOWNLOAD,Above,photo,gallery,steps,text,instruction,Title,Comment,Email,Once,relationship,Many,Drag,cursor,comments,CTRL,diagram,auto,LINQ,connection,Server,Explorer,Just,SkyDrive,pictures,database,Update,moment,option,modification,method,LogOn,user,categories,templates,toolbox


Thank you for your unbelievable support on Negative Zero - Permission to read and write blog for nearly 4 years. Don't forget to like Negative Zero on Facebook.
Blogirific.com Blog Directory





Post(s) you might like to read :

Comments
0 Comments

0 comments:

Post a Comment