This project is designed to help developers learn how to use Entity Framework (EF) Core with C#. It demonstrates how to create, read, update, and delete (CRUD) records in a MySQL database using EF Core, focusing on a simple student management system.
Features Basic CRUD operations with a Student entity. Configuration of database connection using DbContext. Use of option classes to manage database connection details. Support for generic methods to handle different entity types. Getting Started Prerequisites .NET SDK (version 5.0 or later) MySQL Server (version 6.0.2 or later) Entity Framework Core and MySQL packages. You can install these via NuGet:
Before running the application, you need to configure your database connection in the OptionLogin class within the Program.cs file. Modify the following properties according to your MySQL server configuration:
Usage Example The following is an example of how the application operates:
A new Student entity is created and added to the database. All students are retrieved and displayed in the console. The application finds a specific student by name, updates their last name, and saves the changes to the database.
Entity Framework (EF) is a powerful Object-Relational Mapping (ORM) framework for .NET applications. It enables developers to work with databases using .NET objects, simplifying data access and manipulation. In this post, we will explore the benefits of using EF and walk through a practical example using a school database to manage student information.
Entity Framework is an ORM that allows developers to interact with databases using .NET objects. It abstracts the complexities of database interactions, allowing for faster and cleaner code development. EF supports various database systems, including SQL Server, MySQL, and PostgreSQL.
Productivity: EF allows developers to focus on business logic rather than database interactions. The code is cleaner and easier to read. Strongly Typed Queries: With LINQ (Language Integrated Query), developers can write strongly typed queries, reducing runtime errors. Database Migrations: EF supports database migrations, allowing you to evolve your database schema over time without losing data. Change Tracking: EF automatically tracks changes to your entities, simplifying the process of updating records. Relationship Management: EF simplifies managing relationships between entities, making it easier to work with complex data models.
#Setting Up the Context In our example, we have defined two entities: Student and StudentAddress. Here's how the context is set up using DbContext:
This class defines configuration settings, relationships between entities, and sets up the database tables using DbSet properties.
The Fluent API in Entity Framework Core is a way to configure entity properties and relationships using method chaining instead of data annotations. OnModelCreating Method :
EF’s support for database migrations makes it easier to manage changes to the database schema as your application evolves.
With EF migrations, you can alter the database schema without losing data. Migrations allow you to incrementally apply changes to the database, keeping it in sync with your model classes. This is especially useful in collaborative projects where the database may undergo frequent updates.
If you add a new field to your Student model, a migration can automatically update the database schema to include this new field. EF handles the generation of migration scripts, making it straightforward to apply these changes to your database.
EF automatically tracks changes made to entities in memory, which simplifies the process of updating records.
EF’s change tracking feature keeps track of the current and original values of entity properties. When SaveChanges is called, EF generates the necessary SQL to update only those fields that were modified, which is efficient and reduces data redundancy.
If you retrieve a Student entity, modify its Name property, and then call SaveChanges, EF will detect the modification and only update the Name field in the database, leaving other fields untouched.
Entity Framework simplifies database interactions, making it an invaluable tool for developers working with C#. The ability to perform CRUD operations efficiently and manage relationships between entities allows for rapid application development.