Friday, July 21, 2017

Entity Framework : Create, Update and Delete Record

Entity Framework is ORM .net framework, which is used to update, create or delete record from table.

As Per MSDN “Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects

EntityFramework works as a layer between database and business layer (application code) and entity framework maps each database table to entity class ( C# CLR class) and with help of this entity class, developer can perform SQL DML operation like create, modify and delete on mapped database table.

Entity Framework Architecture




Create Record: 

Below code shows how to add new record of department entity

   Department department = newDepartment()
            {
                Name = "Admin"
            };

   DBContext DbContext = new DBContext ();

   DbContext.Departments.Add(department);

   DbContext.SaveChanges();

As you can see, we are performing below steps to create a new record of department with department name ‘Admin’ 
  1. Create new object of Department and set department Name to “Admin”.
  2. Create new DBContext Object.
  3. Add new department object to Department entity set.
  4. Call  saveChanges() method of DBContext Object to add new department record and this method  parse the entity graph and generate SQL script and execute on connected database.
Update Record:

Below code shows how to modified the existing record of department entity

   DBContext DbContext = new DBContext ();

   Department department = DbContext.Departments.FirstOrDefault(d => d.Name == "Admin");

   department.Code = "A";

   DbContext.SaveChanges();

As you can see, below steps are being used to update the existing record of department entity
  1. Create new DBContext Object.
  2. Fetch the existing record of department and load into local memory.
  3. Update desired fields or properties of department record.
  4. Call saveChanges() method of DBContext Object to update department record and this method parse the entity graph and generate SQL script 



Delete Record:

Below code shows how to delete the existing record of department entity

   DBContext DbContext = new DBContext ();

   Department department = DbContext.Departments.FirstOrDefault(d => d.Name == "Admin");

   DbContext.Departments.Remove(department);

   DbContext.SaveChanges();

As you can see, we are performing below steps to delete the existing record of department entity
  1. Create new DBContext Object.
  2. Load the existing record of department into memory.
  3. Remove the record from Department entity Set.
  4. Call SaveChanges() method of DBContext Object to delete the attached department record and this method parse the entity graph and generate SQL script and execute on the connected database.

Thanks for visiting !!


Other Entity framework related Links: 

No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...