Setting Up an ASP.NET Core MVC Project with EF

Setting Up an MVC Application with Entity Framework Core

1. Create a New Project

  • Open Visual Studio or your preferred IDE.
  • Create a new ASP.NET Core MVC Project.
  • Name it as per your preference.

2. Install Required Packages

Use the terminal or Package Manager Console to add the following EF Core packages:

  1. EF Core Core Library: dotnet add package Microsoft.EntityFrameworkCore
  2. SQL Server Provider: dotnet add package Microsoft.EntityFrameworkCore.SqlServer
  3. EF Core Design Tools: dotnet add package Microsoft.EntityFrameworkCore.Design
  4. EF Core Tools for CLI: dotnet add package Microsoft.EntityFrameworkCore.Tools

3. Create the Model Class

Define a model class for the data structure you’ll work with. For example, a Student class:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

4. Create the DbContext Class

The DbContext serves as the bridge to the database.

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }

    public DbSet<Student> Students { get; set; }
}

5. Add Connection String in appsettings.json

Include your database connection string:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=.\\sqlexpress;Database=StudentMDB;Trusted_Connection=True;TrustServerCertificate=True;MultipleActiveResultSets=true"
  }
}

6. Configure Connection in Program.cs

Set up the database connection:

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

7. Create a Controller with Views

Generate an MVC Controller with views using EF:

  • Right-click the Controllers folder in Solution Explorer.
  • Select Add > Controller.
  • Choose MVC Controller with views, using Entity Framework.
  • Select your model class (Student) and DbContext (ApplicationDbContext).
  • Click Add.

8. Add and Apply Migrations

Run the following commands:

  1. Add Migration: dotnet ef migrations add InitialCreate
  2. Update Database: dotnet ef database update

9. Verify Database

Open SQL Server Management Studio (SSMS) or SQL Server Object Explorer in Visual Studio:

  • Locate the database StudentMDB.
  • Confirm that the Students table is created.

10. Update Default Route

Modify the default route in Program.cs to point to your new controller (e.g., Student):

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Student}/{action=Index}/{id?}");

11. Run the Project

Start the project to see your application in action.

12. Using CLI vs. PMC Commands

dotnet ef CLI

  • Command-line interface, ideal for cross-platform use (Linux, macOS, Windows).
  • Example: dotnet ef migrations add InitialCreate dotnet ef database update

Package Manager Console (PMC)

  • Integrated within Visual Studio, more intuitive for beginners.
  • Example: Add-Migration InitialCreate Update-Database

Why Use Each Option?

Aspectdotnet ef CLIPMC Commands
EnvironmentCross-platformWindows (Visual Studio)
Ease of UseRequires CLI knowledgeBeginner-friendly
AutomationIdeal for CI/CD pipelinesManual
IntelliSenseNot availableAvailable

By following this guide, you can set up an ASP.NET Core MVC application with Entity Framework Core. Whether you’re using dotnet ef CLI or PMC, choose the tool that best fits your workflow. Enjoy building robust, database-driven applications!

Leave a Reply