internal static void Main(string[] args) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); using (var db = new StudentSystemContext()) { var material = new Material { Content = "za sega njama" }; db.Materials.Add(material); var course = new Course { Name = "Algorihms", Description = "gadno" }; course.Materials.Add(material); db.Courses.Add(course); var student = new Student { Name = "Pesho", Number = 10000 }; student.Cources.Add(course); db.Students.Add(student); db.SaveChanges(); } }
/* 02. Write a console application that uses the data.*/ /// <summary> /// Mains this instanse. /// </summary> public static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); using (var db = new StudentSystemContext()) { var pesho = new Student { Name = "Pesho", Number = 666 }; db.Students.Add(pesho); db.SaveChanges(); var dbCourse = new Course { Name = "Database Course", Description = "Basic Database operations", Materials = "http://telerikacademy.com/Courses/Courses/Details/98" }; db.Courses.Add(dbCourse); db.SaveChanges(); var course = db.Courses.First(c => c.Name == dbCourse.Name); var student = db.Students.First(s => s.Number == pesho.Number); var hw = new Homework { Content = "Empty Homework", TimeSent = DateTime.Now, CourseId = course.CourseId, StudentId = student.StudentID }; db.Homeworks.Add(hw); db.SaveChanges(); } ListStudents(); }
static void Main() { //Database.SetInitializer(new DropCreateDatabaseAlways<StudentSystemContext>()); Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); StudentSystemContext dbContext = new StudentSystemContext(); var student = new Student() { Name = "Gosho", Number = "15", Courses = new HashSet<Course>() { new Course() { Name = "Entity Framework", Description = "Entity Framework Entity Framework Entity Framework Entity Framework ", Material = "Data base miracles Data base miracles Data base miracles", } }, }; dbContext.Students.Add(student); Console.WriteLine("Student added."); //var students = dbContext.Students.Where(s => s.Name == "Pesho"); //Console.WriteLine(student.Name); dbContext.SaveChanges(); }
static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); var db = new StudentSystemContext(); db.SaveChanges(); }
static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); var db = new StudentSystemContext(); using (db) { var homework = new Homework(); var student = new Student(); var course = new Course(); student.FirstName = "Gancho"; student.LastName = "Minchev"; student.StudentNumber = 12345; course.Name = "Animals"; course.Students.Add(student); course.Homeworks.Add(homework); course.Materials = "Books"; course.Students.Add(student); homework.Content = "African Animals"; homework.Course = course; student.Homeworks.Add(homework); db.SaveChanges(); } }
private static void Main(string[] args) { var db = new StudentSystemContext(); using (db) { db.Courses.Add( new Course { Description = "C# course", Students = new List<Student>() { new Student { Name = "Pencho", Number = 13 } } }); db.SaveChanges(); var students = db.Students.Select(x => new { x.Name, x.Courses }); foreach (var student in students) { Console.WriteLine(student.Name + " participates in " + string.Join(",", student.Courses.Select(x => x.Description))); } } }
static void Main() { Database.SetInitializer( new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); var db = new StudentSystemContext(); db.Students.Add(new Student { Name = "gosho", Number = "123456" }); db.SaveChanges(); }
static void Main(string[] args) { Database.SetInitializer( new MigrateDatabaseToLatestVersion<StudentSystemContext, Data.Migrations.Configuration>()); StudentSystemContext context = new StudentSystemContext(); context.Students.Add(new Student() {FirstName = "Test",LastName = "Student"}); context.SaveChanges(); }
static void Main(string[] args) { var dataBase = new StudentSystemContext(); var course = new Courses { Parent = null, Name = "Database course", Description = "A course that all junior develepers need to pass", Materials = "Google.com"}; dataBase.Courses.Add(course); var studentOnDataBaseCourse = new Students { Name = "Bill Gates", Number = 1 }; var homeWorkForDataBases = new Homework { Content = "Introduction in SQL" }; dataBase.SaveChanges(); }
private static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); using (var studentSystem = new StudentSystemContext()) { studentSystem.Students.Add(new Student() { Name = "Svetlin Nakov", Number = 5000981 }); studentSystem.SaveChanges(); } }
static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion <StudentSystemContext, Configuration>()); var context = new StudentSystemContext(); var course = new Course { Name = "Database" }; var student = new Student { StudentNumber = 31150 }; student.Name = "Pesho Petrov"; course.Students.Add(student); student.Courses.Add(course); var homework = new Homework { SendDate = DateTime.Now, CourseId = 1, StudentId = 1 }; var lecture = new Material { CourseId = 1 }; student.Homeworks.Add(homework); course.Materials.Add(lecture); course.Homeworks.Add(homework); context.Courses.Add(course); context.Students.Add(student); context.Homeworks.Add(homework); context.Materials.Add(lecture); context.SaveChanges(); }
public static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); Random randomGen = new Random(); using (var dbContext = new StudentSystemContext()) { for (int i = 0; i < 20; i++) { Student student1 = new Student() { Name = "Ivan" + i, StudentNumber = randomGen.Next(100000, 999999) }; dbContext.Students.Add(student1); } dbContext.SaveChanges(); } }
public static void Main(string[] args) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); using(var dbContext = new StudentSystemContext()) { var student = new Student { FirstName = "Pesho", LastName = "Peshov", Number = 12345 }; var course = new Course { Name = "DSA", Description = "DSA", Materials = new string[] {"Telerik Academy", "C# Intro"} }; var homework = new Homework { Content = "Let's do it", TimeSend = DateTime.Now }; student.Courses.Add(course); student.Homework.Add(homework); course.Homework.Add(homework); course.Students.Add(student); dbContext.Students.Add(student); dbContext.SaveChanges(); Console.WriteLine("{0} is added!", dbContext.Students.FirstOrDefault().FullName); Console.WriteLine("Ready!"); Console.ReadLine(); } }
static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion <StudentSystemContext, Configuration>()); StudentSystemContext db = new StudentSystemContext(); Student student = new Student { Name = "Dimitar", Number = 1212 }; db.Students.Add(student); Student student2 = new Student("Ivan", 1554); db.Students.Add(student2); Course course = new Course("C#PartII", "C# advanced course", "C# Book"); db.Courses.Add(course); db.SaveChanges(); }
static void Main() { // Task 01. // See StudentSystem.Models project // Task 02. // Got to View -> Server Explorer to see the created database StudentSystemDb Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); using (var context = new StudentSystemContext()) { context.Database.Initialize(true); Student student = new Student() { FullName = "Svetlin Nakov", Number = "12345" }; Course databaseCourse = new Course() { Name = "Database", Description = "SQL, MySQL, SQLite", }; student.Courses.Add(databaseCourse); databaseCourse.Students.Add(student); context.Students.Add(student); context.Courses.Add(databaseCourse); context.SaveChanges(); } // Task 03. // See in -> StudentSystem.Data -> Migrations -> Configuration.cs -> Seed(); }
public static void Main(string[] args) { var studentContext = new StudentSystemContext(); studentContext.Courses .Where(x => x.CourseID == 1) .FirstOrDefault().Students.Add( studentContext.Students .Where(s => s.StudentID == 1) .FirstOrDefault() ); studentContext.Courses .Where(x => x.CourseID == 2) .FirstOrDefault().Students.Add( studentContext.Students .Where(s => s.StudentID == 2) .FirstOrDefault() ); studentContext.Homeworks.Add( new Homework { Title = "Loops", Content = @"ivan-loops-homework.zip", TimeSent = DateTime.Now, StudentID = 1, CourseID = 1 } ); studentContext.Homeworks.Add( new Homework { Title = "Arrays", Content = @"pesho-arrays-homework.zip", TimeSent = DateTime.Now, StudentID = 2, CourseID = 2 } ); var courses = studentContext.Courses.Join( studentContext.Materials, c => c.CourseID, m => m.CourseID, (c, m) => new { CourseID = c.CourseID, Name = c.Name, Description = c.Description, Material = m.Path } ); foreach (var course in courses) { Console.WriteLine("\nID: {0} \nName: {1} \nDescription: {2} \nPath: {3}", course.CourseID, course.Name, course.Description, course.Material); } studentContext.SaveChanges(); }
internal static void Main() { var db = new StudentSystemContext(); db.Database.Delete(); db.Database.Create(); // Add students Console.Write("\nAdding students"); for (int i = 0; i < 10000; i++) { db.Students.Add( new Student() { Name = RandomGenerator.RandomString(RandomGenerator.RandomInt(2, 50)), Number = RandomGenerator.RandomInt(1, 1000000) }); if (i % 250 == 0) { Console.Write("."); db.SaveChanges(); db.Dispose(); db = new StudentSystemContext(); } } // Add courses Console.Write("\nAdding courses"); for (int i = 0; i < 10000; i++) { db.Courses.Add( new Course() { Name = RandomGenerator.RandomString(RandomGenerator.RandomInt(2, 100)), Description = RandomGenerator.RandomString(RandomGenerator.RandomInt(20, 1000)) }); if (i % 250 == 0) { Console.Write("."); db.SaveChanges(); db.Dispose(); db = new StudentSystemContext(); } } // Get all student and courses ids var studentIds = db.Students.Select(x => x.Id).ToList(); var courseIds = db.Courses.Select(x => x.Id).ToList(); // Add homeworks Console.Write("\nAdding homeworks"); for (int i = 0; i < 10000; i++) { db.Homeworks.Add( new Homework() { Content = RandomGenerator.RandomString(RandomGenerator.RandomInt(20, 1000)), TimeSpendMinutes = RandomGenerator.RandomInt(5, 500), StudentId = studentIds[RandomGenerator.RandomInt(0, studentIds.Count)], CourseId = courseIds[RandomGenerator.RandomInt(0, courseIds.Count)] }); if (i % 250 == 0) { Console.Write("."); db.SaveChanges(); db.Dispose(); db = new StudentSystemContext(); } } }
static void Main() { //set and seed database Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentSystemContext, Configuration>()); var context = new StudentSystemContext(); var count = context.Students.Count(); Console.WriteLine(count); //working with database //1.Lists all students and their homework submissions. Select only their names and for each homework - content and content-type. var studentsWithHomeworks = context.Students .Select(s => new { s.Name, Homeworks = s.Homeworks.Select(h => new { h.Content, h.ContentType }).ToList() }).ToList(); foreach (var student in studentsWithHomeworks) { Console.WriteLine("Student: {0}", student.Name); if (student.Homeworks.Count == 0) { Console.WriteLine("no homeworks"); } else { foreach (var homework in student.Homeworks) { Console.WriteLine("Homeworks: {0} - {1}", homework.Content, homework.ContentType); } } } Console.WriteLine(); //2.List all courses with their corresponding resources. Select the course name and description and everything for each resource. //Order the courses by start date (ascending), then by end date (descending). var coursesWithResourses = context.Courses .OrderBy(c => c.StartDate) .ThenByDescending(c => c.EndDate) .Select(c => new { c.Name, c.Description, Resources = c.Resources.Select(r => new { r.Name, r.ResourceType, r.Url }).ToList() }).ToList(); foreach (var course in coursesWithResourses) { Console.WriteLine("Course: {0} - Description: {1}", course.Name, course.Description); foreach (var resourse in course.Resources) { Console.WriteLine("Resourse: {0}; type: {1}; url: {2}", resourse.Name, resourse.ResourceType, resourse.Url); } } Console.WriteLine(); //3.List all courses with more than 1 resource. Order them by resources count (descending), then by start date (descending). //Select only the course name and the resource count. var courses = context.Courses .Where(c => c.Resources.Count > 1) .OrderByDescending(c => c.Resources.Count) .ThenByDescending(c => c.StartDate) .Select(c => new { c.Name, NumberOfResourses = c.Resources.Count }).ToList(); foreach (var course in courses) { Console.WriteLine("Course: {0} - Number of resourses: {1}", course.Name, course.NumberOfResourses); } Console.WriteLine(); //4.List all courses which were active on a given date (choose the date depending on the data seeded to ensure there are results), //and for each course count the number of students enrolled. Select the course name, start and end date, //course duration (difference between end and start date) and number of students enrolled. //Order the results by the number of students enrolled (in descending order), then by duration (descending). var activeCourses = context.Courses .Where(c => c.StartDate <= DateTime.Now && c.EndDate >= DateTime.Now) .OrderByDescending(c => c.Students.Count()) .ThenByDescending(c => SqlFunctions.DateDiff("DAY", c.StartDate, c.EndDate)) //or EntityFunctions.DiffDays(c.StartDate, c.EndDate) .Select(c => new { c.Name, c.StartDate, c.EndDate, Duration = SqlFunctions.DateDiff("DAY", c.StartDate, c.EndDate), NumberOfStudents = c.Students.Count() }).ToList(); foreach (var course in activeCourses) { Console.WriteLine("Course: {0}; start date: {1}; end date: {2}; duration: {3} days; number of students: {4}", course.Name, course.StartDate, course.EndDate, course.Duration, course.NumberOfStudents); } Console.WriteLine(); //5.For each student, calculate the number of courses she’s enrolled in, the total price of these courses and //the average price per course for the student. Select the student name, number of courses, total price and average price. //Order the results by total price (descending), then by number of courses (descending) //and then by the student’s name (ascending) var students = context.Students .OrderByDescending(s => s.Courses.Sum(c => c.Price)) .ThenByDescending(s => s.Courses.Count) .ThenBy(s => s.Name) .Select(s => new { StudentName = s.Name, NumberOfCourses = s.Courses.Count, TotalPrice = s.Courses.Sum(c => c.Price), AveragePricePerCourse = s.Courses.Average(c => c.Price) }).ToList(); foreach (var student in students) { Console.WriteLine("Sudent: {0}; number of courses: {1}; total price of courses: {2}; average price per course: {3}", student.StudentName, student.NumberOfCourses, student.TotalPrice, student.AveragePricePerCourse); } //change database: add table Licenses and populate it //create few Licenses License ccbyncsa = new License() { Name = "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" }; License telericLicense = new License() { Name = "HTML Basics course by Telerik Academy under CC-BY-NC-SA license" }; context.Licenses.AddOrUpdate(ccbyncsa, telericLicense); //extract Resourses from the database var lab = context.Resources.Find(1); var lecture = context.Resources.Find(2); //add relationships between licenses and resourses lab.Licenses = new HashSet<License> { ccbyncsa }; lecture.Licenses = new HashSet<License> { ccbyncsa, telericLicense }; context.Resources.AddOrUpdate(lab, lecture); context.SaveChanges(); }