This is my first article of MVC 4 Development series, which is based on basic guidance of implementation of the code first approach, initially this project is build on Console Application to avoid complexities for the starters.

Before starting the drill, I would first like to tell you that all of my example will base on Restaurant Management System and i will try to keep this example consistent through out this series.

Following is an ERD of the system, I hope the naming conventions are self explanatory to all the viewers.


Create a new console application project in your visual studio, by going to File > New Project in menu bar or Ctrl + Shift + N



Select Console Application from Visual C# templates, named it "RMSConsoleApplication" and click OK.

Remember in Code-First Approach we have models of each entity in ERD, therefore for implementing models you need a separate class file, to do so, Right Click on the project in solution explorer and go to  Add sub menu and select class from menu OR Ctrl + Shift + A and Select Class from list, name it "SpecialityType.cs" and click OK.

Then place this code.
Then do the same drill for rest of the entities
After creating models from entities we need to add Database Context layer in which all the models are being use to define EF Entities. To generate Database Context you first need to reference EF package in you project. To do so, best way is to Right Click on the project and Go to Manage NuGet Packages option, this will open a window, in window type "EntityFramework" in search box this will download and references EF in to your project.
Now just below <configsection> in your app.config file add following code
Then Create a new class file with name "RestaurantContext.cs" and place following code.
And this namespaces above this file.
We have almost completed 90% of our task here, rest of the 10% percent is to implement seeding so that it can have some sample data when ever it would drops and creates database. For implementing seeding we need to create an other class file with name "RestaurantInitializer.cs" and place following code in it.
I now its lots of code above that hard to understand but what it does, it uses objects of model to store data to database through EF, I have populated sample data here.
And in last, we will write code to fetch data from database through EF and LINQ. To do so open program.cs file and place this code
Now run the project, if you properly followed me you will get result just like below.

And if you open SQL Server Object Explorer by shortcut key Ctrl + \ , Ctrl + S Or Menu>View>SQL Server Object Explorer and expanding the database, you will see self generated tables with primary and foreign key relations.

Thats all for today, feel free to comment.