Discovering the power of Entity Framework Core (EF Core) through a Code First approach unlocks a rich, dynamic, and efficient way to interact with your databases. This hands-on tutorial will guide you through setting up an EF Core project, creating models, and working with databases using the celebrated Code First methodology. Let's dive in!

EF Core, a modern, extensible, and cross-platform version of Entity Framework, is designed to work with various database systems such as SQL Server, PostgreSQL, MySQL, and SQLite, just to name a few. It simplifies database interactions, allowing developers to focus on their core business logic. But what makes EF Core a game-changer is its support for Code First, enabling you to define your schema and models through C# classes rather than SQL scripts.

Setting Up Your Environment
Before we start, ensure you have .Net Core 3.1 or later installed on your machine. Install the Entity Framework Core tools globally via NuGet packet manager using the command: dotnet tool install --global dotnet-ef

Next, create a new .Net Core console application: dotnet new console -n EfCoreTutorial Replace 'EfCoreTutorial' with the desired name for your project.
Creating the Model

Create a new directory named 'Models' in your project and add a new C# class named 'Blog'. This will be our basic model representing a simple blog with an id, url, and name.
namespace EfCoreTutorial.Models
{
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
}
}
Creating the DbContext Class
In the root directory of your project, create a new file named 'ApplicationDbContext.cs'. This class will interact with the database, manage our entities, and execute database operations.

using Microsoft.EntityFrameworkCore;
namespace EfCoreTutorial
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
}
}
Configuring the Database
In the 'Program.cs' file, configure the connection string and add-migration command for creating your database schema.
Connect to a database using the 'UseSqlServer' method, specifying your connection string. Then, create an instance of the 'ApplicationDbContext' class and call the 'Add-Migration' command to generate SQL scripts based on the 'Blog' model. Ensure you have the correct connection string based on your chosen database system.

Running Migrations
Navigate to the terminal, run dotnet ef migrations script to update your database schema based on the generated SQL scripts.









Now that your database is set up, we can start performing CRUD operations using the Code First approach.
Performing CRUD Operations
Add methods to the 'Program.cs' file for creating, reading, updating, and deleting blog entries. Use LINQ queries to interact with the database through the 'ApplicationDbContext' instance.
Experience the power of Code First with Entity Framework Core and boost your productivity while writing clean, maintainable, and efficient code. Happy coding!
As you've seen, Entity Framework Core significantly simplifies working with databases using its Code First methodology. With its rich feature set and cross-database support, EF Core is a valuable tool in any developer's toolbox. Now that you're comfortable with the basics, explore more advanced topics, such as migrations, concatenating queries, or even integrating EF Core with your favorite API framework. Keep learning, and until next time, happy coding!