public Major Create(Major major)
        {
            if (major == null)
            {
                throw new Exception("Major cannot be NULL");
            }
            if (major.Id != 0)
            {
                throw new Exception("Major.Id has to be zero");
            }
            _context.Majors.Add(major);
            var rowsAffected = _context.SaveChanges();

            if (rowsAffected != 1)
            {
                throw new Exception("Create failed");
            }
            return(major);
        }
        public Student Create(Student student)
        {
            if (student == null)
            {
                throw new Exception("Student cannot be NULL");
            }
            if (student.Id != 0)
            {
                throw new Exception("Student.Id must be zero!");
            }
            _context.Students.Add(student);
            var rowsAffected = _context.SaveChanges();

            if (rowsAffected != 1)
            {
                throw new Exception("Create Failed");
            }
            return(student);
        }
示例#3
0
        public async Task <Major> Remove(int id)
        {
            var major = _context.Majors.Find(id);

            if (major == null)
            {
                return(null);
            }
            int count = await _context.Students.Where(s => s.MajorId == major.Id).CountAsync();

            _context.Majors.Remove(major);
            var rowsAffected = _context.SaveChanges();

            if (rowsAffected != 1)
            {
                throw new Exception("Remove Failed");
            }
            return(major);
        }