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()); }
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()); } }
public async Task AddAsync(T entity) { dbContext.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Added; await dbContext.SaveChangesAsync(); }