Table of Contents
- 1: Create a new ASP.NET Core project
- 2: Add Entity Framework Core
- 3: Create the model and context
- Step 4: Configure the database in the appsettings.json file
- Step 5: Register the DbContext in Program.cs
- Step 6: Create a Controller
- Step 7: Create the views
- Step 8: Create the database
- Step 9: Run the application
- Conclusion
We created this guide to help new developers and experienced professionals familiarize themselves with the capabilities offered by ASP.NET Core and Entity Framework Core 9. This article includes a practical and detailed demo project designed to explore the new features introduced in these technologies, providing a useful and concrete reference for real applications.
With the release of Entity Framework Core 9, the framework introduces significant improvements, including support for primitive collections, optimized query performance, and smoother integration with the .NET 9 runtime. These important innovations further consolidate the prominence of ASP.NET Core, which even in 2025, presents itself as an ideal framework for developing robust and flexible web applications: our sample application illustrates how to best combine its capabilities with the new features of EF Core 9.
The guide is designed to be accessible both to beginners who want to discover these technologies for the first time, and to experienced developers who want to update their skills or better understand the new features of this version. By following this article, you will be able to create a complete application, understand how to configure a project, and make the most of the innovations in ASP.NET Core and Entity Framework Core 9.
In the sections of this article, we will develop a complete example of a trial application using ASP.NET Core 9 and Entity Framework 9, including context configuration, model creation, CRUD operations (Create, Read, Update, Delete), and a simple web interface to interact with the database.
1: Create a new ASP.NET Core project
Open the terminal and use the following command to create a new ASP.NET Core project:
1 2 |
dotnet new webapp -n EFCoreDemo cd EFCoreDemo |
2: Add Entity Framework Core
Install the necessary packages for Entity Framework Core using the command:
1 2 |
dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Tools |
3: Create the model and context
Create a folder called Models and add a Product.cs class and an AppDbContext.cs class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Models/Product.cs using System.ComponentModel.DataAnnotations; namespace EFCoreDemo.Models { public class Product { public int Id { get; set; } [Required] public string Name { get; set; } [Range(0.01, 10000)] public decimal Price { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
// Models/AppDbContext.cs using Microsoft.EntityFrameworkCore; namespace EFCoreDemo.Models { public class AppDbContext : DbContext { public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } public DbSet<Product> Products { get; set; } } } |
Step 4: Configure the database in the appsettings.json file
Add the connection string for your SQL Server database in the appsettings.json file.
1 2 3 4 5 |
{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EFCoreDemoDb;Trusted_Connection=True;" } } |
Step 5: Register the DbContext in Program.cs
Register the DbContext in the Program.cs file as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using Microsoft.EntityFrameworkCore; using EFCoreDemo.Models; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); builder.Services.AddControllersWithViews(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); |
Step 6: Create a Controller
Create a folder called Controllers and add a ProductsController.cs controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
// Controllers/ProductsController.cs using Microsoft.AspNetCore.Mvc; using EFCoreDemo.Models; namespace EFCoreDemo.Controllers { public class ProductsController : Controller { private readonly AppDbContext _context; public ProductsController(AppDbContext context) { _context = context; } // GET: Products public IActionResult Index() { var products = _context.Products.ToList(); return View(products); } // GET: Products/Create public IActionResult Create() { return View(); } // POST: Products/Create [HttpPost] [ValidateAntiForgeryToken] public IActionResult Create(Product product) { if (ModelState.IsValid) { _context.Products.Add(product); _context.SaveChanges(); return RedirectToAction(nameof(Index)); } return View(product); } // GET: Products/Edit/5 public IActionResult Edit(int id) { var product = _context.Products.Find(id); if (product == null) return NotFound(); return View(product); } // POST: Products/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public IActionResult Edit(int id, Product product) { if (id != product.Id) return NotFound(); if (ModelState.IsValid) { _context.Update(product); _context.SaveChanges(); return RedirectToAction(nameof(Index)); } return View(product); } // GET: Products/Delete/5 public IActionResult Delete(int id) { var product = _context.Products.Find(id); if (product == null) return NotFound(); return View(product); } // POST: Products/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public IActionResult DeleteConfirmed(int id) { var product = _context.Products.Find(id); _context.Products.Remove(product); _context.SaveChanges(); return RedirectToAction(nameof(Index)); } } } |
Step 7: Create the views
Create a folder called Views/Products and add the following views:
Index.cshtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-3"><h1>Products</h1></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-4"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-5"><a asp-action="Create">Create New</a></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-6"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-7"><table class="table"></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-8"> <thead></div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-9"> <tr></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-10"> <th>Name</th></div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-11"> <th>Price</th></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-12"> <th></th></div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-13"> </tr></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-14"> </thead></div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-15"> <tbody></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-16"> @foreach (var product in Model)</div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-17"> {</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-18"> <tr></div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-19"> <td>@product.Name</td></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-20"> <td>@product.Price</td></div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-21"> <td></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-22"> <a asp-action="Edit" asp-route-id="@product.Id">Edit</a> |</div><div class="crayon-line" id="urvanov-syntax-highlighter-6795e55d6bd35017218534-23"> <a asp-action="Delete" asp-route-id="@product.Id">Delete</a> </td> </tr> } </tbody> </table> |
Create.cshtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@model EFCoreDemo.Models.Product <h1>Create Product</h1> <form asp-action="Create"> <div class="form-group"> <label asp-for="Name"></label> <input asp-for="Name" class="form-control" /> <span asp-validation-for="Name" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Price"></label> <input asp-for="Price" class="form-control" /> <span asp-validation-for="Price" class="text-danger"></span> </div> <button type="submit" class="btn btn-primary">Create</button> </form> |
Edit.cshtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@model EFCoreDemo.Models.Product <h1>Edit Product</h1> <form asp-action="Edit"> <input type="hidden" asp-for="Id" /> <div class="form-group"> <label asp-for="Name"></label> <input asp-for="Name" class="form-control" /> <span asp-validation-for="Name" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Price"></label> <input asp-for="Price" class="form-control" /> <span asp-validation-for="Price" class="text-danger"></span> </div> <button type="submit" class="btn btn-primary">Save</button> </form> |
Delete.cshtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@model EFCoreDemo.Models.Product <h1>Delete Product</h1> <h3>Are you sure you want to delete this?</h3> <div> <h4>@Model.Name</h4> <p>Price: @Model.Price</p> </div> <form asp-action="DeleteConfirmed"> <input type="hidden" asp-for="Id" /> <button type="submit" class="btn btn-danger">Delete</button> <a asp-action="Index" class="btn btn-secondary">Cancel</a> </form> |
Step 8: Create the database
Run the migrations to create the database using the following commands:
1 2 |
dotnet ef migrations add InitialCreate dotnet ef database update |
Step 9: Run the application
Finally, run the application with the command:
1 |
dotnet run |
Visit https://localhost:5001/Products in your browser to see the app in action.
Conclusion
This project demonstrates the main features of Entity Framework Core 9.0, including CRUD operations and integration with an ASP.NET Core application. Make sure you have SQL Server running and properly configured to test the project.