Entity Framework : Code First Vs Database First

code first vs database first
entity framework code first vs database first

Entity framework is an opensource object-relational mapping framework for ADO.NET. when it is introduced it is part of the .NET framework but since it’s version 6 and above it is separated from the NET framework. you need to install it from NuGet package manager.

Entity framework provides an ease to developers to work with data using the object of domain-specific classes. it provides the connection between domain classes and database objects.

Code First

Code first is a workflow that creates a database that is not available, and code first will create it based on entity classes and configuration. a developer can also update the database using into the change in model, is called database migration.

  • it is an approach that has full control over the code rather than database activity.
  • In code first, you can do all the database operations from the code. everything is depending on code.
  • In code first, you need to create POCO entities as a data model. “POCO entity is a class that doesn’t depend on any framework-specific base class”

Create database using code first

Create a new application and install the entity framework from NuGet package manager. once an application is created and package installed then add the following class into an application.

public class Employee
{
   public int EmpID { get; set; }
   public string Name { get; set; }
}
public class EmployeeContext : DbContext
{
   public EmployeeContext() : base("EmployeeContext")
   {
   }
   public DbSet Employees { get; set; }
}

the following class defines Employee class that makes domain model and EmployeeContext class that is entity framework code first approach.

Now let’s check about database access. the following model example illustrates how to perform data access using code first.

using System;
using System.Data.Entity;

namespace EFDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new EmployeeContext())
            {
                db.Employees.Add(new Employee { Name = "Alex Jhons" });
                db.SaveChanges();

                foreach (var emp in db.Employees)
                {
                    Console.WriteLine(emp.Name);
                }
            }
        }
    }

    public class Employee
    {
        public int EmpID { get; set; }
        public string Name { get; set; }
    }
    public class EmployeeContext : DbContext
    {
        public DbSet Employees { get; set; }
    }
}

Database First

The database first approach provides the alternative to creating POCO entities classes from the existing database.

It is used when database ready to use, then entity framework creates POCO entities automatically for you.

  • if you have already created a database then this is the best approach to start.
  • Update or modify the database manually and then update the model from a database.
  • Using this approach you can visualize tables and relationships between them using EDMX.
  • simple to create data models and proffered for large applications.

Here Database is already designed and created so no need to worry about database. here we are already post brief article for creating application using entity framework.

you can check for step by step tutorial for creating application using database first.

Actually most of beginners of .NET developer don’t know about code first as well database first, that is why we publish this article. I hope this article will help you to understand approaches for code first and database first. we are here to help you if you have any issues or queries. comments on your suggestions and issues we try to resolve it.

What is your reaction?

0
Excited
0
Happy
0
In Love
1
Not Sure
0
Silly

You may also like

Comments are closed.

More in MVC