public void AddStudent(string firstName, string LastName) { var student = new Student() { StudentId = 1, FirstName = firstName, LastName = LastName }; student.Enrollments = new List <Enrollment>() { new Enrollment() { CourseCourseId = 1 }, new Enrollment() { CourseCourseId = 2 }, new Enrollment() { CourseCourseId = 3 }, new Enrollment() { CourseCourseId = 4 } }; using (var dbcontext = new ModelFirstContainer()) { dbcontext.Students.Add(student); dbcontext.SaveChanges(); } }
public void AddCourse(string name, string credits) { var course = new Course() { CourseId = 1, Name = name, Credits = credits }; using (var dbcontext = new ModelFirstContainer()) { dbcontext.Courses.Add(course); dbcontext.SaveChanges(); } }
static void Main(string[] args) { using (var context = new ModelFirstContainer()) { var student = context.Students.Where(w => w.Name == "Mark").FirstOrDefault(); var courseList = context.Courses.Where(w => w.Name != "Mech").ToList(); //Query Syntax var studentsEnrolled = (from s in context.Students join e in context.Enrollments on s.StudentId equals e.StudentStudentId select s.Name).Distinct().ToList(); //Method syntax var studentsEnrolled1 = context.Students.Join(context.Enrollments, s => s.StudentId, e => e.StudentStudentId, (s, e) => new { s.Name }).Distinct().ToList(); //Left Join /* * Select S.Name, COUNT(E.EnrollmentId) from Students S * LEFT JOIN Enrollments E ON S.StudentId=E.StudentStudentId * GROUP BY S.Name */ var LeftJoin = (from s in context.Students join e in context.Enrollments on s.StudentId equals e.StudentStudentId group s by s.Name into es from e1 in es.DefaultIfEmpty() select new { es.Key, count = es.Count() }).Distinct().ToList(); foreach (var student1 in LeftJoin) { Console.WriteLine("{0}-{1}", student1.Key, student1.count); } /* * foreach(var course in courseList) * { * var enroll = new Enrollment() * { * Student = student, * Course = course * }; * context.Enrollments.Add(enroll); * context.SaveChanges(); * * } */ } // Console.ReadKey(); }
public Student GetStudent(int StudentId) { using (var dbcontext = new ModelFirstContainer()) { dbcontext.Configuration.LazyLoadingEnabled = false; //Eager loading //var student = dbcontext.Students.Include("Enrollments").Where(w => w.StudentId == StudentId).FirstOrDefault(); //var student = dbcontext.Students.Where(w => w.StudentId == StudentId).FirstOrDefault(); var student = (from stud in dbcontext.Students where stud.StudentId == StudentId select stud).FirstOrDefault(); //Explicit Loading dbcontext.Entry(student).Collection(s => s.Enrollments).Load(); return(student); } }
static void Main(string[] args) { using (var db = new ModelFirstContainer()) { Department d = new Department(); d.DepartmentNo = 121; d.Name = "Rajeev"; Worker w = new Worker(); w.WorkerID = 123; w.Name = "John"; w.Place = "Jaipur"; HashSet <Worker> s = new HashSet <Worker>(); s.Add(w); d.Workers = s; db.Departments.Add(d); try { db.SaveChanges(); Console.WriteLine("The records are added..!!!!!"); Console.ReadKey(); } catch (Exception e) { } } }