Пример #1
0
        public int UpdateStudent(Student student, List <Course> courses)
        {
            using (var db = new MiniDbContext())
            {
                var oldStudent = db.Students
                                 .Include("Courses")
                                 .Include("Address")
                                 .SingleOrDefault(s => s.StudentId == student.StudentId);


                foreach (var item in oldStudent.Courses.ToList())
                {
                    oldStudent.Courses.Remove(item);
                }

                db.Entry(oldStudent).CurrentValues.SetValues(student);
                db.Entry(oldStudent.Address).CurrentValues.SetValues(student.Address);

                foreach (var item in courses)
                {
                    oldStudent.Courses.Add(item);
                }

                return(db.SaveChanges());
            }
        }
        public async Task <IActionResult> PutSource(int id, Source source)
        {
            if (id != source.Id)
            {
                return(BadRequest());
            }

            _context.Entry(source).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SourceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #3
0
        public int AddStudent(Student student, List <Course> courses)
        {
            using (var db = new MiniDbContext())
            {
                student.Courses = courses;
                db.Students.Add(student);

                //Ne dodaje child tabeli objekte, vec ih samo vezuje za parenta.
                foreach (Course course in courses)
                {
                    db.Entry(course).State = EntityState.Unchanged;
                }

                return(db.SaveChanges());
            }
        }
Пример #4
0
 public async Task AddAsync(T entity)
 {
     dbContext.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Added;
     await dbContext.SaveChangesAsync();
 }