Welcome to the CodeYourCraft ASP .NET Tutorial! In this lesson, we'll dive into the Code First Approach, a powerful strategy that simplifies database creation and development in ASP .NET applications. Let's get started! šÆ
In a Code First Approach, you write your models (representing database tables) in code first, and Entity Framework (EF) generates the corresponding database schema automatically. This approach offers several advantages:
Before we dive into the Code First Approach, let's ensure your development environment is ready:
Now that our environment is set up, let's create our first model:
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
// Navigation property for related data
public ICollection<Order> Orders { get; set; }
}š Note: Our Product model has properties for Id, Name, and Price. We also defined a navigation property Orders to represent related data (orders that contain this product).
Now, let's configure our database context:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure database schema
}
}š Note: We defined a DbSet for our Product model in the ApplicationDbContext. The OnModelCreating method is where we configure the database schema.
Finally, let's configure the database schema for our Product model:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasKey(p => p.Id);
modelBuilder.Entity<Product>()
.Property(p => p.Name)
.IsRequired()
.HasMaxLength(100);
modelBuilder.Entity<Product>()
.Property(p => p.Price)
.HasColumnType("decimal(18, 2)");
}š Note: We configured the Id as the primary key, defined that the Name should be required and have a maximum length of 100 characters, and set the Price's data type to decimal(18, 2).
Now that our models and database context are set up, we can create the database using migrations:
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0" />dotnet.json file:{
"runtimes": {
...
"Microsoft.AspNetCore.App": {
"version": "5.0.0"
}
}
}dotnet ef migrations add InitialCreate
dotnet ef database update
Now that our database is created, let's add some real-world data:
using Microsoft.EntityFrameworkCore;
var context = new ApplicationDbContext();
// Insert sample data
var product1 = new Product { Name = "Laptop", Price = 1000M };
var product2 = new Product { Name = "Smartphone", Price = 500M };
context.Products.AddRange(product1, product2);
context.SaveChanges();š Note: We created an instance of ApplicationDbContext, added some sample data, and saved the changes to the database.
Congratulations! You've completed the Code First Approach lesson. You now have a solid foundation for working with databases using the Code First Approach in ASP .NET.
What is the primary purpose of the OnModelCreating method in the ApplicationDbContext class?