Files
AspDiary/DiaryApp/Data/ApplicationDbContext.cs
2026-02-03 23:02:37 +01:00

57 lines
1.7 KiB
C#

using DiaryApp.Models;
using Microsoft.EntityFrameworkCore;
namespace DiaryApp.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<DiaryEntry> DiaryEntries { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<DiaryEntry>().HasData(
new DiaryEntry
{
Id = 1,
Title = "Went Hiking",
Content = "Went hiking with Joe!",
DateCreated = DateTime.Parse("2026-01-01 10:32")
},
new DiaryEntry
{
Id = 2,
Title = "Went Shoping",
Content = "Went Shoping with Joe!",
DateCreated = DateTime.Parse("2026-01-10 08:32")
},
new DiaryEntry
{
Id = 3,
Title = "Went Swimming",
Content = "Went Swimming with Joe!",
DateCreated = DateTime.Parse("2026-01-15 15:42")
},
new DiaryEntry
{
Id = 4,
Title = "Went Biking",
Content = "Went Biking with Joe!",
DateCreated = DateTime.Parse("2026-01-17 12:00")
}
);
}
// Four steps to add a table
// 1. Create a Model Class
// 2. Add DB-set
// 3. add-migration AddDiaryEntryTable
// 4. update-database
}
}