As an application developer, the need to interact with databases is a given. This is where the .NET Entity Framework comes in - a powerful object-relational mapping (ORM) library that enables .NET developers to work with databases using .NET objects and collections instead of SQL queries. Among its various approaches, Entity Framework Code First is a popular method that allows developers to create their database schema based on simple POCO (Plain Old CLR Objects) classes. Let's dive into an example of how to use ASP.NET Core with Entity Framework Code First.

Before we start, ensure you have the latest .NET SDK and Visual Studio installed. For this example, we'll create a simple blog with posts and comments using ASP.NET Core MVC.

Setting Up Your ASP.NET Core Project
First, create a new ASP.NET Core MVC project. In your terminal, run:

dotnet new mvc -n BlogExample
Installing Required Packages

Let's install the required packages: Microsoft.EntityFrameworkCore.Sqlite (for the SQLite database engine), and Microsoft.EntityFrameworkCore.Design (for the EF Core tools).
Run this command in your project folder to install them: dotnet add package Microsoft.EntityFrameworkCore.Sqlite Microsoft.EntityFrameworkCore.Design
Creating Your Models

We'll create two models: Post.cs and Comment.cs. In your project root, create a new folder named Data, and inside it, a new class named Models.
First, create the Post model:
public class Post { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public List<Comment> Comments { get; set; } = new List<Comment>(); }

Now, create the Comment model:
public class Comment { public int Id { get; set; } public string Name { get; set; } public string Content { get; set; } public int PostId { get; set; } public Post Post { get; set; } }









Configuring the DbContext
Next, we need to create a DbContext class to manage our database. In the Data folder, create a new class named ApplicationDbContext:
public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<Post> Posts { get; set; } public DbSet<Comment> Comments { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Post>().HasMany(p => p.Comments).WithOne(c => c.Post).HasForeignKey(c => c.PostId); } }
Registering the DbContext
Now, in your Startup.cs file, register the DbContext in the ConfigureServices method:
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
Creating the Database
To create the database using Code First, we'll add a new migration and update the database:
dotnet ef migrations add InitialCreate
dotnet ef database update
Creating Controllers and ViewModels
Now, create PostController.cs and CommentController.cs in the Controllers folder. Implement CRUD operations for posts and comments, using the DbContext to interact with the database. Also, create corresponding view models and views in the Views/Shared folder.
With these steps, you've created a simple ASP.NET Core MVC application using Entity Framework Code First to manage your database. This approach allows you to focus on your models and lets Entity Framework take care of the database schema. Happy coding!
Now that you have a solid grasp of ASP.NET Core MVC with Entity Framework Code First, consider exploring more of its features, likeigrations, and queries. And remember, keeping your skills up-to-date is key in the ever-evolving world of software development. Happy coding!