Wednesday, 27 August 2014

Code First Approach using Entity Framework 4.0 Sample example using ASP.NET,C#.NET

Hi friends,in this article I will explain about Entity Framework Code First example  using ASP.NET,C#.NET.
I already explained in the previous articles about Telerik RadGrid Grouping -Drag and Drop a column header to group by that column in ASP.NET using C#/VB.NETHow to show validation control's Error messages in Alert box in ASP.NET and How to Upload Multiple Files Using FileUpload Control in ASP.NET using C#/VB.NET

Code First: In the Code First approach, you avoid working with visual model designer (EDMX) completely. You write your POCO classes first and then create database from these POCO classes. Developers who follow the path of Domain-Driven Design (DDD) principles prefer to begin by coding their classes first and then generating the database required to persist their data.
Create the Class Library project in Visual Studio 2010 as below.

Install Entity Framework as shown in the below figure.

After that below references will be added

Create Student entity classes as below (You can use Entity Framework 4.1/4.3/5.0 for this example.):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace CodeFirstEntityFramework
{
    [Table("Student")]
    public class Student
    {
        [Key]
        public int StudentID { get; set; }
        [Column("Gender")]
        [MaxLength(50)]
        public string Gender { get; set; }
    }
}

Add your connectionstring in App.config like below
<connectionStrings>
                                <add name="Student_DBConnectionString"
        connectionString="Data Source=Test;Initial Catalog=Student_EntityFramework;User Id=sa;password=12345;Max Pool Size=75000"
        providerName="System.Data.SqlClient"/>
                </connectionStrings>

Add Context.cs class and write the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace CodeFirstEntityFramework
{
    public class Context : DbContext
    {
        public Context()
            : base("Student_DBConnectionString")
        {

        }
        public DbSet<Student> Student_DbSet { get; set; }
    }
}

Open Package Manager Console
Tools---> Nuget Package Manager-- -> Package Manager Console

Now we need to run some commands in the Package Manager console
To go to Package Manager Console Go to Tools >> Library package Manager and click on Package Manager Console which shows a console.
Here we have to type “Enable-Migrations” which creates the Migrations folder and Metadata of the database we are going to create.
Once we enable the Migrations run the following command “Add-Migration Initial”
Once the above step is done we have to run the following command which completes the database creation “Update-Database”

Then your database will be created with Student Table in the database.See in the below figure.
You can download the code by clicking on the below Download image. 

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.