Page 9 - EntityFrameworkNotesForProfessionals-1-10
P. 9
Name = "Jupiter",
AverageDistanceFromSun = 778.5
};
context.Planets.Add(jupiter);
context.SaveChanges();
}
In this example we create a new Planet with the Name property with the value of "Jupiter" and the
AverageDistanceFromSun property with the value of 778.5
We can then add this Planet to the context by using the DbSet's Add() method and commit our changes to the
database by using the SaveChanges() method.
Or we can retrieve rows from the database:
using(var context = new PlanetContext())
{
var jupiter = context.Planets.Single(p => p.Name == "Jupiter");
Console.WriteLine($"Jupiter is {jupiter.AverageDistanceFromSun} million km from the sun.");
}
Section 1.3: What is Entity Framework?
Writing and managing ADO.Net code for data access is a tedious and monotonous job. Microsoft has provided an
O/RM framework called "Entity Framework" to automate database related activities for your application.
Entity framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives
developers an automated mechanism for accessing & storing the data in the database.
What is O/RM?
ORM is a tool for storing data from domain objects to the relational database like MS SQL Server, in an automated
way, without much programming. O/RM includes three main parts:
1. Domain class objects
2. Relational database objects
3. Mapping information on how domain objects map to relational database objects(e.x tables, views & stored
procedures)
ORM allows us to keep our database design separate from our domain class design. This makes the application
maintainable and extendable. It also automates standard CRUD operation (Create, Read, Update & Delete) so that
the developer doesn't need to write it manually.
GoalKicker.com – Entity Framework Notes for Professionals 5