Let's start with Entity Framework Code First Approach

Entity Framework code first approach is introduced in v4.1 release. Entity Framework allow create custom entity (POCO class) and based on that you can easily create or modify your database. Entity Framework gives freedom to change DB structure through code. You can use Entity Framework code first approach whether you have an existing database or new because Entity Framework tools can generate the classes and properties as per existing database tables and columns. Entity Framework Migrations play important role in creating or modify database. Automatic migration makes database schema changes easily when any entity change. We will discuss more about Entity Framework code first migration in our next article Entity Framework code first approach in asp.net MVC.
In this article I took simple example that best suit for newbie in Entity Framework code first approach.
1. Create a console application

2. Add Student.cs and Department.cs classes to project. As per POCO classes, single department have multiple students. That relation i declared with navigation property.
public class Student
     public Student()
     public int Id { get; set; }
     public string Name { get; set; }
     public DateTime? DateOfBirth { get; set; }
     public string EmailId { get; set; }
     public string Address { get; set; }
     public string City { get; set; }

     public Department Department { get; set; } // Navigation Property
public class Department
    public Department()
    public int Id { get; set; }
    public string DepartmentName { get; set; }

    public List Students { get; set; } // Navigation property
3. Add Entity Framework reference to project

After download, it will show pop up to add reference in project

Add namespace System.ComponentModel.DataAnnotations to existing class for Data Annotations. After Data Annotation our class will look like

4. Add StudentDbContext.cs to project which will inherit DbContext (System.Data.Entity), copy-paste below code. A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type.
public class StudentDbContext : DbContext
     public DbSet<Student> Students { get; set; }
     public DbSet<Department> Departments { get; set; }
5. We done almost all things. now, time to test Code First Approach. Add Student to interact will Database.
public class Program
    static void Main(string[] args)
        using (var context = new StudentDbContext())
            var student = new Student()
                Name = "Test",
                EmailId = "test@gmail.com",
                Address = "Test Address",
                City = "Test City"
Entity Framework code first creates database automatically in our Local Sql Server because i haven't specify connectionstring. In my application EF generates DB having name EFCodeFirstApp.StudentDbContext  in my local Sql Server. It creates two table Department and Student. You can see Department_Id as foreign key automatically added  in Student table.

Entity Framework code first approach itself big area. I try to demonstrate only small part of it and I hope, you enjoy learning. 

Download Sample code

Keep visiting for our next Entity Framework code first approach article.


Popular posts from this blog

12 opensource and free asp.net MVC gridview contols

Scheduling background jobs using Hangfire in ASP.NET Core

Difference ExecuteNonQuery vs ExecuteScalar vs ExecuteReader in asp.net