Challenge: Save Data
Challenge yourself by saving data to a school database.
We'll cover the following...
Overview
Using the project below, perform the tasks highlighted in the requirements section.
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);
}
}
}
Save data
After the required code is implemented, execute the project by ...
Ask