CRUD Operation with EF Core Using Class Library and Console Application
What is Entity Framework Core
Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology.
EF Core can serve as an object-relational mapper (O/RM), which:
- Enables .NET developers to work with a database using .NET objects.
- Eliminates the need for most of the data-access code that typically needs to be written.
- We can either use Code First or DB First Approach to work
Pros of Entity Framework Code First
- You can create a database and required tables from business entities
- It is recommended for small applications that does not involve extensive data processing
- You can specify the collections for eager loading and the serialization of data
- It provides full access over the code and you can do modifications easily in the code
Cons of Entity Framework Code First
- You need to write the code related to the creation of database
- If there is any change in database after the creation, you need to do it in the business entity class of code and run the application to update the database or by using package manager console
- It is difficult to manage the database through code, therefore, it is not recommended in data extensive applications where you need to process large amount of data and have complex logics to buildup or maintain the data
- Any manual changes will be lost if you update the code from application
Pros of Entity Framework DB First
- Graphical User Interfaces are available to create database and tables, which makes the process easier
- It is preferred for the large and extensive data-driven applications
- It is easier to create keys and relationships without writing extra code for it
- It can use an existing database
- Visual Studio provides easy access to configure database via Edmx files
Cons of Entity Framework DB First
- If there is any change in database, model class needs to be extended with the same properties
- Creating and managing of keys and relationships requires more coding
- It is difficult to maintain or update Edmx file if the database is large
ADO.Net Vs Entity Framework
Create a Class Library Project with EF 6.0 Code First Approach
- Create a Project of Type Class Library with the name EntityFramework.Data
- Add following Nuget Packages
- Microsoft.EntityFrameworkCore (6.0.6 or latest stable package)
- Microsoft.EntityFrameworkCore.SQLServer (6.0.6 or latest stable package)
- Microsoft.EntityFrameworkCore.Tools (6.0.6 or latest stable package - used to run the migration)
- Add a new class and name it DemoDbContext, make it public and inherit it from DbContext (available from namespace Microsoft.EntityFrameworkCore)
- Add a constructor
- Override the OnConfiguring method
- Right click on the project -> Add new folder with the name Entities
- Inside the Model - Add 2 Classes Employee and EmployeeEducation (1-M relationship) and add some properties. ID column in both table is primary key and it's auto generated column.
- Now add property of DbSet type for both tables in the class DemoDbContext
- If there are multiple projects in the solution, then set this project as startup project (Right click the project name in solution and click "Set as Startup Project"
- Run the below command to add migration (This will add the Tables in SQL Database)
- To run the migration, open Package Manager Console (View - > Other Windows - > Package Manager Console)
- Run the command Add-Migration InitialMigration (InitialMigration is the name of Migration, you can give any meaningful name here)
- Run Update-Database to reflect the Changes in the Database
- Create a Class CRUDManager and write methods for CRUD Operation
Create a Console Application to Consume class library methods for CRUD operation
- Add reference of class library project
- Set Console application as start up project
Some Useful Keywords
OrderBy - This LINQ method is used to order the data on the basis of a certain column in ascending order
var employee = demoDbContext.Employees.Where(x => x.Name == "Abhimanyu")
.AsNoTracking().OrderByDescending(s => s.Address)
.Last();
OrderByDescending- This LINQ method is used to order the data on the basis of a certain column in descending order.
var employee = demoDbContext.Employees.Where(x => x.Name == "Abhimanyu")
.AsNoTracking().OrderByDescending(s => s.Address)
.Last();
ThenBy or ThenByDescending - This method is used to sort the data in ascending or descending order based on second column onwards. It can only be applied after OrderBy or OrderByDescending
var employee = demoDbContext.Employees.Where(x => x.Name == "Abhimanyu")
.AsNoTracking().OrderBy(s => s.Name).ThenByDescending(s => s.Address)
.Last();
Reference Links: https://dotnettutorials.net/lesson/entity-framework-core/
Source Code: https://github.com/kumarabhimanyu/EFCoreDemo
Comments
Post a Comment