Object Relational Mapping
Object Relational Mapping
(ORM)
Lecture 03
CRUD Operations
● First, you create an instance of the entity you want to add to the database. This entity should correspond to
the type T of the DbSet<T> collection.
● For example, if you have a Blog entity and a corresponding Blogs DbSet in your DbContext, you would create
a new Blog instance.
● Once you have the entity instance, you use the Add method of the DbSet<T> collection to add it to the
context.
● This operation adds the entity to the in-memory collection managed by Entity Framework, marking it as an
entity to be inserted into the database when SaveChanges() is called.
● After adding all the entities you want to persist, you call the SaveChanges() method on the database context.
● Entity Framework then generates and executes SQL commands necessary to insert the new entity into the
database.
● The entity is added to the appropriate database table, and if successful, the changes are committed to the
database.
Update Operation
● When you retrieve entities from the database using EF, such as through LINQ queries or navigation properties,
EF automatically tracks these entities.
● Each entity is associated with its own state, known as EntityState, which indicates whether the entity is Added,
Modified, Deleted, or Unchanged.
● When you modify the properties of an entity that is being tracked by EF, EF automatically marks the EntityState
of that entity as Modified.
● This automatic change tracking allows EF to keep track of the changes made to the entity's properties without
any explicit intervention from the developer.
● After making modifications to one or more entities, you call the SaveChanges() method on the DbContext to
persist these changes to the database.
● EF generates and executes the necessary SQL commands, including UPDATE statements, to update the
corresponding records in the database based on the modified entities.
● Only the properties that have been modified are included in the UPDATE statement, resulting in efficient
database updates.
Delete Operation
Read Operation
When using Entity Framework (EF), the DbSet class represents a
collection of entities within a database context, and it is indeed derived
from IQueryable.
This allows you to use LINQ (Language-Integrated Query) to query
against DbSet, which is then translated into SQL queries by the EF API to
interact with the underlying database.
Search Operation
We can use the Find() method of DbSet to Search the entity based on the
primary key value.
Lets’ see those in practical…….
Now you are at this stage…,
Next move into database first,
Select Cities table > Edit rows
> Add Two cities with city id 1
and 2 > save the changes.
areaObj.AreaName = Console.ReadLine();
areaObj.CityId=Convert.ToInt32(Console.ReadLine());
areaObj.Pincode= Console.ReadLine();
Next, We have to specify our context class object
DbSet property that declared in our context class
1. Here is the DbSet property declared in our context class > Areas (Which is
type DbSet)
foreach (Area a in db.Areas) //Areas is our DbSet property which is in our context class & db is our
object in our context class created above
Console.WriteLine("{0}\t{1}\t{2}\t{3}",a.AreaId,a.AreaName,a.Pincode,a.City.CityName); //display
areas
break;
Suppose you want to display CityName,
a.City.CityName
Let’s run this part and confirm whether its working fine…
1. Go to Project > Database first properties > Specify the startup object >
CRUD> Save
2. Run the project…
3. Give option 1
5. Hit enter > it will take time bcz it is adding data to your database
else
{
Remove area object from the database. Here remove method is called it will mark as
Entity state is deleted.
db.Areas.Remove(areaObj);
db.SaveChanges(); //Delete sql querry will be executed in the
database
}
First display the available data and check the database
Finally play with Records and check how database is
updating………..