Challenge Solution: Save Data
Explore how to implement data saving techniques using Entity Framework Core by inserting and deleting related entities like departments, instructors, courses, and students while maintaining relationships. Understand how to manage one-to-many and many-to-many relationships effectively and handle data removal with proper context state changes.
Solution
The project below demonstrates the solution to the challenge:
using Microsoft.EntityFrameworkCore;
using SaveData.Models.Entities;
namespace SaveData.Models.Data
{
public class SchoolContext : DbContext
{
public SchoolContext()
{
}
public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
{
}
public virtual DbSet<Instructor> Instructors { get; set; }
public virtual DbSet<Student> Students { get; set; }
public virtual DbSet<Department> Departments { get; set; }
public virtual DbSet<Course> Courses { get; set; }
public virtual DbSet<StudentCourse> StudentCourses { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("data source=output/School.db");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Instructor>()
.HasOne(d => d.Department)
.WithMany(i => i.Instructors)
.HasForeignKey(inst => inst.DepartmentCode);
modelBuilder.Entity<StudentCourse>().HasKey(sc => new { sc.StudentId, sc.CourseId });
modelBuilder
.Entity<StudentCourse>()
.HasOne(sc => sc.Student)
.WithMany(s => s.StudentCourses)
.HasForeignKey(sc => sc.StudentId);
modelBuilder
.Entity<StudentCourse>()
.HasOne(sc => sc.Course)
.WithMany(c => c.StudentCourses)
.HasForeignKey(sc => sc.CourseId);
}
}
}
Solution to save data
Click the “Run” button in the ...