27
DecIntroduction to Entity Framework
ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with a relational database. It enables developers to deal with data as objects and properties. Using the Entity Framework, developers issue queries using LINQ, then retrieve and manipulate data as strongly typed objects using C# or VB.Net.
The Entity Framework’s ORM implementation also provides services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals. ADO.NET Entity Framework is targeted for developing Enterprise applications that can support MS SQL databases as well as other databases like as Oracle, DB2, MySql, and many more. To learn the entity framework first we need to know what is orm. I am going to give an overview of ORM.
Entity Framework is a robust Open-Source and the Object-Relational Mapping (ORM) Framework for .NET applications that are crafted for the.NET developers to work with the relational data manipulation by using the domain-specific objects without worrying much about the focusing on the certain database tables and their set of columns where the real the data is being stored. It means that the Entity Framework completely destroys the need of writing the data access code that developers generally need to write.
What is O/RM?
O/RM is an acronym that stands for object/relational mapping. Basically, an O/RM framework is used to persist model objects in a relational database and retrieve them. It uses metadata information to interface with the database. This way, your data-layer code knows nothing about the database structure. The O/RM tool becomes middleware that completely hides the complexity.
The heart of O/RM is the mapping—the mapping technique is what binds the object and relational worlds. By mapping, you express how a class and its properties are related to one or more tables in the database. This information is used by the O/RM tool’s engine to dynamically build SQL code that retrieves data and transforms it into objects. Similarly, by tracking changes to objects’ properties, it can use mapping data to send updates back to the database. The mapping information is generally expressed as an XML file. As an alternative, some O/RM tools use attributes on the classes and their properties to maintain mapping data.
The inappropriate mismatch of the data can be solved by using a mechanism that takes the multiple objects of our application and stores them in a form of rows and a column in the database in a certain manner. That is exactly what the ORM framework does. ORM Stands for "Object Relational Mapper" and that tools decouple the object model from the database by creating the constructive abstraction layer between each of them seamlessly. The ORM model translates the domain classes into the rows and columns in the database tables. And it also separates the representation logic of the data stored from the implementation details of the store itself accordingly.
Advantages of Entity Framework
Productivity
Entity Framework can take up to 35 percent of the entire application code. It makes the developer’s life easier than ever. In spite of its limitations, the designer integrated into Visual Studio dramatically simplifies the mapping process.
Apart from the work and efforts productivities, if the development is done seamlessly with a short span of time using an entity framework it will have an advantage of achieving higher chances of cost-effectiveness as it will save much cost compared to the different other methodologies.
Maintainability
Since you have fewer lines of code to fetch data from the database, the fewer lines of code you have to maintain. This is particularly true in big projects where the application structure is huge and concurrent developers are working on certain models making it hard to maintain the codebase versioning.
Performance
The complexity of O/RM introduces an obvious slowdown in performance. In the entity framework first request to fetch data is a little bit slow but after that is fast to fetch data from the database.
Transactions
ORM does all possible automation as transactions while querying the data or storing the data periodically. We can also customize transaction management practices to make the most of it out of the entity framework.
Entity Framework Models
Microsoft’s Entity Framework evolved from a methodology known as Entity-Relationship Modeling (ERM). An ERM defines a schema of entities and their relationships with one another. Entities are not the same as objects. Entities define the schema of an object, but not its behavior. So, an entity is something like the schema of a table in your database, except that it describes the schema of your business objects. There are three models in the entity framework:
CONCEPTUAL MODEL
The conceptual model is where you describe the model classes. This file is split into two main sections: the first is a container that lists all the entities and the relationships that are managed by Entity Framework, and the second contains a detailed description of their structure.
STORAGE MODEL
The storage model is the equivalent of the conceptual model, but it describes the database organization. Not only is this file conceptually similar to the previous one, but it also uses the same XML nodes. Unlike the conceptual model, it isn’t possible to split this model into several physical files. The first section of this file lists all the tables, views, stored procedures, and foreign keys that are affected. The second section describes the items listed in the first node.
Regarding tables and views, the columns and primary keys are described. When it comes to stored procedures, input and output parameters are described. The description of a foreign key contains information about the table involved, the cardinality, and then delete and update rules.
MAPPING MODEL
The mapping file is completely different. Its job isn’t to describe something but to compensate for the differences that exist between the two previous models. This is where the real magic of mapping happens: you map a class to one or multiple tables, map one table to one or multiple classes, define inheritance mapping, and map stored procedures for both updates and object retrieval.
There is only one important node in this file: it associates the class to a table and can be repeated more than once to ensure that a class can be mapped against multiple tables, and vice versa. Like the storage description file and unlike the conceptual file, the mapping file can’t be split into multiple files.