Entity Framework

EDMX (Entity Data Model XML) is an XML file which contains all the mapping details of how your objects map with SQL tables.

The EDMX file is further divided into three sections: CSDL, SSDL, and MSL.

  • SSDL (Storage Schema Definition Language) defines the mapping with your RDBMS data structure.

  • MSL (Mapping Schema Language) connects the CSDL and SSDL.

  • CSDL (Conceptual Schema definition language) is the conceptual abstraction which is exposed to the application.

What is the importance of T4 in Entity Framework?
T4 files are the heart of EF code generation. The T4 code templates read the EDMX XML file and generate C# behind code. This C# behind code is nothing but your entity and context classes.

How can we read records using Entity Framework classes?
In order to browse through records you can create the object of the context class and inside the context class you will get the records.

For instance, in the below code snippet we are looping through a customer object collection. This customer collection is the output given by the context class CustomermytextEntities.

CustomermytestEntities obj = new CustomermytestEntities();
foreach (Customer objCust in obj.Customers)
{}

How can we add, update, and delete using EF?
Create the object of your entity class, add it to the data context using AddObject method, and then call the SaveChanges method.

CustomermytestEntities obj = new CustomermytestEntities();
Customer objCust = new Customer();
objCust.CustomerCode = "1001";
obj.Customers.AddObject(objCust);
obj.SaveChanges();

If you want to update, select the object, make changes to the object, and call AcceptAllChanges.

CustomermytestEntities objContext = new CustomermytestEntities();
Customer objCustomer = (Customer)objContext.Customers.FirstOrDefault();
objCustomer.CountryCode = "NEP";
objContext.AcceptAllChanges();

If you want to delete, call the DeleteObject method as shown in the below code snippet:

CustomermytestEntities objContext = new CustomermytestEntities();
Customer objCustomer = (Customer)objContext.Customers.FirstOrDefault();
objContext.DeleteObject(objCustomer);