code first approach in asp.net mvc application

Introduction

I show you earlier that how we Connect MVC App with Entity framework database first approach .Now i will show you how to Connect MVC App with Entity framework Code first approach.

Creating structure

Create Asp.net MVC application. open web.config and add a connection string. Below is example of connection string

  <add name="ConString" connectionString="Data Source=your Sql server instance or IP;Initial Catalog=database name;User ID=userid;Password=password" providerName="System.Data.SqlClient"/>>

Add a new folder in Models name it 'Structure'. Add a class in structure folder name it Product

Product class references

Now add following references in product class

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using yourproject.Models.Structure;

Now add product class properties

Product Class Code

 public class Products
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long Product_ID { get; set; }
		public string Product_Name { get; set; }
        public decimal Product_Price { get;set;}
        public DateTime Publish_Date { get; set; }
        public virtual List ProductImage { get;set;}
    }

code Explanation

Generating identity ProductId we decorate our product_ID with [DatabaseGenerated(DatabaseGeneratedOption.identity)]. public virtual List ProductImage { get;set;} is a foreign key reference for ProductImage Table.

Now add a new class in structure folder and name it ProductImages

productImages class references

Now add following references in productImages class

using System.ComponentModel.DataAnnotations;
using ClassifiedWebsite.Models;
using System.ComponentModel.DataAnnotations.Schema;

Now add ProductImages class properties

productImages Class properties

public class ProductImages
    {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long id { get; set; }
        public double ProductId { get; set; }
        public string ImageName { get; set; }
        public string ImagePath { get; set; }
    }

Implement Mapping

Now add a new class in Models folder name it DataContext. This class inherit from Database context class and responsible to communicate with our database server like sql server

Add following reference in Datacontext class

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using yourproject.Models.Structure;

DataContext class

 public class DataContext:DbContext
    {
        public DataContext()
            : base("ConString")
        {

        }
        public DbSet products { get;set;}
        public DbSet Product_Images { get;set;}
    }

base("ConString") is your connectionString name which you define in connectionString name attribute

Now navigate to Tools-> Library package Manager-> Package Manager console. And write following command in console.

Enable-Migrations -ContextTypeName DataContext

if above command successfully run than you see this message Code First Migrations enabled for project MultipleFileUploads in console. You also see visual studio add a folder name Migrations in solution explorer.. Expand the node of this folder and open c# file configuration. set AutomaticMigrationsEnabled to true and save it.

Now After Enabling migration you need to update your database. so write a new command to update or making new database.

Update-Database -Verbose

That's all. open your sql server and navigate to targeted database you find tables of product and productImages successfully created.

when ever you make change in c# classes that you define in structure folder you need to run Update-Database command in package manager console.