advertisement
Data Handling in C# - ADO. NET & Entity Framework
Data Handling
Data handling in C# is possible with the help of files or by using database technology. ADO.NET is a database technology component of the .NET Framework that is used to connect an application system and a database server. ADO stands for ActiveX Data Objects. The Microsoft ADO.NET technology comprises a set of classes to interact with data sources such as XML files and databases and handle data access. The data is then utilized in .NET applications.
1. ADO. NET
ADO.NET Uses XML for storing and transferring data among applications. It is an industry-standard and also provides fast access to data for desktop and distributed applications. The main advantage of ADO. NET is its scalability and interoperability.
Features of AD0.NET:
Some of the features of ADO. NET are as follows:
- ADO.NET is a single object-oriented API. Although there are different data providers to work with different data sources, the programming model for all the data providers works in the same way Thus, by knowing one data provider, the user can change class names and connection strings. The ADO.NET classes are object-oriented. Thus, they can be easily used and understood.
- The code and classes in ADO.NET are managed. The Common Language Runtime (CLR) is responsible for language independence and automatic resource management.
- To work in visual form, .NET offers ADO.NET Components and data-bound control. These components can be used without extensive coding. The results are extremely quick.
- Performance and scalability play a major role when developing Web-based applications and services. The disconnected cached data in XML helps to achieve optimal performance and scalability.
Following are some categories of .NET applications that use ADO.NET for connecting to a database, executing commands, and retrieving data:
- ASP.NET Web applications
- Windows applications
- Console applications
2. Entity Framework
Entity Framework (EF) refers to a collection of technologies in ADO.NET that support the development of data-oriented software applications. It is an open-source Object Relational Mapper (ORM) framework intended for .NET applications. Entity Framework enables developers to work with data by using objects of domain-specific classes. There is no emphasis on the underlying database tables and columns for storing this data. By using the Entity Framework, developers can achieve a high level of abstraction when working with data. Further, they can create data-oriented applications and maintain them using a lesser amount of code as compared to traditional applications.
In data-oriented applications, it is required to model entities and relationships as well as the logic of the business problems being solved and also work with data engines for storing and retrieving the data. The data can span multiple storage systems, with each having its protocols. Further, applications working with a single storage system should be able to balance the storage system requirements against the requirement for writing efficient application code that is easily maintainable. All these requirements are fulfilled in the Entity Framework.
Through the Entity Framework, data can be dealt with as domain-specific objects and properties, for instance, customers and customer addresses. Entity Framework offers the advantage for developers to work at a high level of abstraction when dealing with the data as there is no necessity to focus on the basic database tables and columns storing the data. Further, data-oriented applications can be created using less code when compared to traditional applications, and the code can be easily maintained.
Lists of various components of the Entity Framework architecture.
- Conceptual Model: This is independent of the database table design. It includes model classes along with their associations.
- Storage Model: Represents the design model of a database and comprises views, tables, and stored procedures along with their associations.
- Mapping: Has details on how to map the conceptual and storage models.
- ADO. NET Data Provider: Interacts with the database
- Object Service: Refers to the primary access point to obtain and return the database's data. It takes care of converting the returned data into an entity object structure. This is known as the materialization process.
- UNQ-to-Entities (L2E): Refers to a query language that returns the conceptual model's entities.
- Entity SQL: Denotes another query language, which applies only to EF 6. It is similar to L2E but is harder to learn than the latter.
- Entity Client Data Provider: Converts Entity SQL or L2E queries into SQL, which the database can comprehend. It also interacts with the ADO.NET data provider.
The following are some features of EF:
- EF creates an Entity Data Model (EDM) based on Plain Old CLR Object (POCO) entities with get/set properties of various data types. The data model describes the structure of data, as entities and relationships, regardless of how the data will be stored. This model is used for querying or saving entity data to the underlying database.
- EF enables the use of LINOQ queries using C# for retrieving data from the underlying database. LINQ queries are translated to the database-specific query language, for example, SQL for a relational database, by the database provider. EF also allows executing raw SQL queries directly to the database.
- Automatic transaction management occurs when querying or saving data. EF also provides options for customizing transaction management.
- EF keeps track of changes that have occurred to instances of entities (Property values) that are given to the database.
- EF offers the first level of caching out of the box. Thus, repeated querying will result in data returning from the cache rather than affecting the database. This can speed up data performance in applications.
3. EF Core and EF 6
As of today, there are two different versions of Entity Framework, namely, Entity Framework Core 5.0 and Entity Framework 6.
Note: EF Core 6 is not yet stable.
EF Core is an object-database mapper for .NET and accommodates LINQ queries, updates, and schema migrations among other features. EF Core works with databases such as SQL Server/Azure SQL Database. SQLite, Azure Cosmos DB, MySQL, and so on.
Both versions have almost the same goal but differ in features in many ways. Now, new features of Entity Framework are added only in Entity Framework Core, but many features of Entity Framework 6 have not been added in Entity Framework Core.
EF Core 5.0 cannot be used with .NET Framework applications. This is a breaking change.
Some of the features of Entity Framework 6 are as follows:
- Entity splitting: Allows mapping of one data model class to multiple database tables. It is useful in situations where data is stored across many tables that exhibit one-to-one relationships or one-to-zero relationships.
For example, Student personal information is stored in one table, and Student class performance Is stored in another table and they are both related to each other. In such a case, one data model can be used for both tables. - Creating/Updating a model from the database: Allows to generate a database. To do this, one should open a .edmx file to display the model diagram right-click the design surface anywhere, and then, select Update Model from the Database.
- Graphical visualization of a model: Allows to edit entity data model (.edmx) using design tools. This feature is supported only in Entity Framework 6 and not in Entity Framework Core.
- Stored procedure mapping: Entity Framework 6 Code-First provides the facility to create and use stored procedures for add, and update. and delete operations when the Save Changes() method is called. One can map a model by calling the MapToSt or Procedures() method to map an entity with the default stored procedures. Three default stored procedures matching the naming convention will be automatically created - for example, sp_Insert Book, sp_Updat eBook, and sp_DeleteBook. One can also pass their own stored procedure names if they do not want to use the default ones.
Features of Entity Framework Core 5.0 are as follows:
- Alternate keys: EF Core uses SQL to generate and read databases so an alternate key serves as a unique alternate identifier for each entity instance in addition to the primary key
- Key generation: This feature is used to generate keys that are required before inserting any data in the database.
- SQL queries and Composing with LINO: Entity Framework Core enables users to utilize raw SQL queries when working with a relational database. These queries are useful if the query one requires cannot be expressed using LINQ. They can return regular entity types or keyless entity types that are part of the model. Raw SQL can be executed on entity objects, provided sQL return attributes match entity properties.
For example,
var books = Context. Books
.FromSql ("SELECT * FROM dbo. Books" )
.ToList (); - Filtered include: In Entity Framework Core 5.0 onwards, the Include method supports filtering of entities included.
For example,
var books = context.Books
. Include (e => e.Titles. Where (p => p. Author. Contains ( "James") ))
.ToList ();
This query will return books together with each associated title, but only when the author contains "James".
It is recommended that developers should use EF Core on .NET 5.0 for all new applications.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0