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:
- EF Core Core Library: dotnet add package Microsoft.EntityFrameworkCore
- SQL Server Provider: dotnet add package Microsoft.EntityFrameworkCore.SqlServer
- EF Core Design Tools: dotnet add package Microsoft.EntityFrameworkCore.Design
- 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:
- Add Migration: dotnet ef migrations add InitialCreate
- 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.
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:
Why Use Each Option?
Aspect | dotnet ef CLI | PMC Commands |
---|---|---|
Environment | Cross-platform | Windows (Visual Studio) |
Ease of Use | Requires CLI knowledge | Beginner-friendly |
Automation | Ideal for CI/CD pipelines | Manual |
IntelliSense | Not available | Available |
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!