Skip to content

Instantly share code, notes, and snippets.

@bpesquet
Created October 25, 2022 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpesquet/b4d34d91ffbe013dbff630a099acab42 to your computer and use it in GitHub Desktop.
Save bpesquet/b4d34d91ffbe013dbff630a099acab42 to your computer and use it in GitHub Desktop.
using MvcUniversity.Data;
namespace MvcUniversity.Models;
public class SeedData
{
public static void Init()
{
using (var context = new UniversityContext())
{
// Look for existing content
if (context.Students.Any())
{
return; // DB already filled
}
// Add students
Student carson = new Student
{
FirstName = "Alexander",
LastName = "Carson",
EnrollmentDate = DateTime.Parse("2016-09-01"),
};
Student alonso = new Student
{
FirstName = "Meredith",
LastName = "Alonso",
EnrollmentDate = DateTime.Parse("2018-09-01"),
};
Student anand = new Student
{
FirstName = "Arturo",
LastName = "Anand",
EnrollmentDate = DateTime.Parse("2019-09-01"),
};
Student barzdukas = new Student
{
FirstName = "Gytis",
LastName = "Barzdukas",
EnrollmentDate = DateTime.Parse("2018-09-01"),
};
context.Students.AddRange(
carson,
alonso,
anand,
barzdukas
);
// Add instructors
var abercrombie = new Instructor
{
FirstName = "Kim",
LastName = "Abercrombie",
HireDate = DateTime.Parse("1995-03-11")
};
var fakhouri = new Instructor
{
FirstName = "Fadi",
LastName = "Fakhouri",
HireDate = DateTime.Parse("2002-07-06")
};
var harui = new Instructor
{
FirstName = "Roger",
LastName = "Harui",
HireDate = DateTime.Parse("1998-07-01")
};
// Add departments
var mathematics = new Department
{
Name = "Mathematics",
Administrator = fakhouri
};
var engineering = new Department
{
Name = "Engineering",
Administrator = harui
};
var economics = new Department
{
Name = "Economics",
Administrator = harui
};
// Add courses
Course chemistry = new Course
{
Id = 1050,
Title = "Chemistry",
Credits = 3,
Department = engineering,
Instructors = new List<Instructor> { abercrombie, harui }
};
Course microeconomics = new Course
{
Id = 4022,
Title = "Microeconomics",
Credits = 3,
Department = economics,
Instructors = new List<Instructor> { harui }
};
Course macroeconmics = new Course
{
Id = 4041,
Title = "Macroeconomics",
Credits = 3,
Department = economics
};
Course calculus = new Course
{
Id = 1045,
Title = "Calculus",
Credits = 4,
Department = mathematics,
Instructors = new List<Instructor> { fakhouri }
};
context.Courses.AddRange(
chemistry,
microeconomics,
macroeconmics,
calculus
);
// Add enrollments
context.Enrollments.AddRange(
new Enrollment
{
Student = carson,
Course = chemistry,
Grade = Grade.A
},
new Enrollment
{
Student = carson,
Course = microeconomics,
Grade = Grade.C
},
new Enrollment
{
Student = alonso,
Course = calculus,
Grade = Grade.B
},
new Enrollment
{
Student = anand,
Course = chemistry,
},
new Enrollment
{
Student = anand,
Course = microeconomics,
Grade = Grade.B
},
new Enrollment
{
Student = barzdukas,
Course = chemistry,
Grade = Grade.C
}
);
// Commit changes into DB
context.SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment