Code first using Entity Framework in MVC4

In order to use code first in MVC4, we have first to create models and then context for that models then migrate various Models. We are starting with an empty MVC4 application in visual studio and then we will move. Steps are :

Step 1

Create a empty MVC4 Project, Directory structure of the application will be as:

Step 2 (adding Model classes)

First Model Class (Employee.cs)


using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace codeFirstApp.Models
{
    public class Employee
    {
        [Key] // for primary key
        [Column(Order = 1)]
        public int Id { get; set; }

        public string employeeName { get; set; }
        public int employeeSalary { get; set; }
    }
}

Second Model Class (Product.cs)


using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace codeFirstApp.Models
{
    public class Product
    {
        [Key] // for primary key
        [Column(Order = 1)]
        public int Id { get; set; }

        public string productName { get; set; }
        public string productCategory { get; set; }

    }
}

Our Models folder now will have two files as shown above (Product.cs and Employee.cs)

Step 3 (adding database context class to the Model folder)

File name is ContextClasses.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace codeFirstApp.Models
{
    public class ContextClasses : DbContext
    {
        public DbSet<Employee> emp { get; set; }
        public DbSet<Product> prod { get; set; }
    }
}

Step 4 (Adding Connection in web.config)

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  
  <!-- ADDING CONNNECTION STRING -->
      <connectionStrings>
    <add name="ContextClasses" connectionString="Data Source=MyPc-PC\SQLEXPRESS;Initial Catalog=practiceDB;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
<!-- FINISHED ADDING CONNECTION STRING -->

Where :

ContextClasses Name of the context class
Data Source Your database server host
Initial Catalog Name of the Sql Server Database
providerName Name of the connection provider

Step 4 (migrating Models to database)

Open Package Manager Console by going to Tools > Library Package Manager > Package Manager Console. Now follow commands :
Command 1 (enabling migrations)


PM> Enable-Migrations
Command 2


PM> Add-migration initialMigration

This command will create a new file in your migrations folder with some code.
Command 3 (Updating database)


PM> Update-Database

After successfully execution of this command, your tables will be created and sample of Sql Server Management Studio will be displayed as :
Untitled

OUR SOLUTION EXPLORER WILL HAVE DIRECTORY STRUCTURE AS:
Untitled

Share

You may also like...