ADO.NET Entity Framework (EF) is an Object-Relational Mapping (ORM) framework provided by Microsoft as part of the .NET platform. It simplifies data access by allowing developers to work with data in an object-oriented way, instead of dealing with raw SQL queries. Entity Framework enables developers to interact with a relational database using objects and LINQ queries, which can automatically translate those queries into SQL commands behind the scenes.
This model abstracts database interactions and provides a higher-level approach for querying and manipulating data, making it easier to work with databases and focusing on business logic rather than SQL.
Person entity might represent a person in your application and map to a Person table in your database.The Entity Data Model (EDM) is a conceptual framework used by Entity Framework to model your data. The EDM allows you to map the objects in your application to the relational data in the database. It consists of the following components:
These components work together to define how your application objects are mapped to relational data in the database.
CSDL is a part of the Entity Data Model (EDM) and defines the conceptual schema of your data. It describes the entities, relationships, and data types of your application. The conceptual schema is independent of any specific database, allowing your application to operate at a higher level of abstraction.
In CSDL, entities are defined as classes, and the relationships between them are defined (such as one-to-many or many-to-many). This schema is what developers primarily work with when using Entity Framework, as it represents the objects your application uses.
The CSDL file describes:
Customer entity might map to the Customers table in your database.Here’s an example of how a CSDL might look for a Customer and Order entity in a simple database model:
<Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="MyModel">
<!-- Define Customer Entity -->
<EntityType Name="Customer">
<Key>
<PropertyRef Name="CustomerID" />
</Key>
<Property Name="CustomerID" Type="Int32" Nullable="false" />
<Property Name="FirstName" Type="String" Nullable="false" />
<Property Name="LastName" Type="String" Nullable="false" />
</EntityType>
<!-- Define Order Entity -->
<EntityType Name="Order">
<Key>
<PropertyRef Name="OrderID" />
</Key>
<Property Name="OrderID" Type="Int32" Nullable="false" />
<Property Name="OrderDate" Type="DateTime" Nullable="false" />
<Property Name="CustomerID" Type="Int32" Nullable="false" />
</EntityType>
<!-- Define the Relationship Between Customer and Order -->
<Association Name="CustomerOrders">
<End Type="MyModel.Customer" Role="Customer" Multiplicity="1" />
<End Type="MyModel.Order" Role="Orders" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Customer">
<PropertyRef Name="CustomerID" />
</Principal>
<Dependent Role="Orders">
<PropertyRef Name="CustomerID" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
In the above example:
Customer entity has properties like CustomerID, FirstName, and LastName.Order entity has properties like OrderID, OrderDate, and CustomerID.CustomerOrders) is established between the Customer and Order entities using the CustomerID property.The CSDL represents the conceptual data model your application will use, and Entity Framework can automatically generate the corresponding SQL queries for database interaction.
SSDL (Store Schema Definition Language):
MSL (Mapping Specification Language):
Entity Framework uses the following approach:
DbContext class is responsible for managing entities and querying the database.DbContext and can be saved back to the database.ADO.NET Entity Framework simplifies data access by providing an Object-Relational Mapping (ORM) framework that allows developers to interact with databases using objects instead of raw SQL queries. The Entity Data Model (EDM) is at the heart of Entity Framework, consisting of the Conceptual Schema Definition Language (CSDL), which represents the data model in your application. CSDL allows for clear definition of entities, properties, and relationships, making it easier to work with complex data models and abstracting away the underlying database structure.
This abstraction makes development easier, as you no longer need to manually write SQL for every interaction with the database. Instead, you can focus on your business logic while Entity Framework handles the database communication.
Open this section to load past papers