Пример #1
0
        public async Task <Major> Create(Major major)
        {
            if (major.Id != 0)
            {
                throw new Exception("Major must be ZERO");
            }
            _context.Majors.Add(major);
            var rowsAffected = await _context.SaveChangesAsync();

            if (rowsAffected != 1)
            {
                throw new Exception("Create Failed");
            }
            return(major);
        }
        private readonly eddbContext _context;// when you use readonly you can only set the value in a constructor
        // this means we can use and put it in a foreach loop. we can put generic list or data in it.// this only means you can only read the data. and no other class could have access to this method. This is also the dbcontext instance that allows us to pull info from there.

        // making this method into a async
        public async Task <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 rowsAffect = await _context.SaveChangesAsync();// it's important to remember to do SaveChanges

            if (rowsAffect != 1)
            {
                throw new Exception("Create failed!");
            }
            return(student);
        }
Пример #3
0
        }                                                   // for the async method

        public async Task <Student> Create(Student student)
        {
            if (student == null)
            {
                throw new Exception("Student Connot be NULL");
            }
            if (student.Id != 0)
            {
                throw new Exception("Student ID must be 0");
            }
            _context.Students.Add(student);
            var rowsAffected = await _context.SaveChangesAsync();   // make sure to check for async versions of the methods!!!!!

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