Quando eseguo questo metodo con EF 6 lo studente è stato aggiornato!
public async Task Update(Student student)
{
context.Entry(student).State = EntityState.Modified;
await context.SaveChangesAsync();
}
Quando eseguo questo metodo con EF 7, nel database non è cambiato nulla!
Cosa ho sbagliato? Non voglio recuperare prima l'entità per aggiornarlo!
AGGIORNARE
Ho provato / catturato il SaveChanges e ho ricevuto questo messaggio di errore:
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Students_Schoolclasses_SchoolclassId". The conflict occurred in database "TGBData", table "dbo.Schoolclasses", column 'Id'.
The statement has been terminated.
È un problema quando si imposta lo stato dell'entità WHOLE su modificato, quando una delle sue proprietà (es. Student.SchoolclassId) è una chiave esterna?
AGGIORNAMENTO 2
public class Student
{
public Student()
{
StudentsTests = new HashSet<StudentTest>();
StudentsSubjects = new HashSet<SubjectStudent>();
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ISet<StudentTest> StudentsTests { get; set; }
public ISet<SubjectStudent> StudentsSubjects { get; set; }
public Schoolclass Schoolclass { get; set; }
public int SchoolclassId { get; set; }
}
public class Schoolclass
{
public Schoolclass()
{
Students = new HashSet<Student>();
Tests = new HashSet<Test>();
}
public int Id { get; set; }
public string Number { get; set; }
public ISet<Student> Students { get; set; }
public ISet<Test> Tests { get; set; }
public Schoolyear Schoolyear { get; set; }
public int SchoolyearId { get; set; }
}
Quando viene immesso il metodo di aggiornamento, le proprietà FirstName / LastName degli studenti hanno i nuovi valori!
Il tuo errore sta affermando che il tuo Studente ha SchoolclassId e quel valore non esiste nella tabella esterna che è l'Id nella tabella Schoolclasses.
Controlla il valore di SchoolclassId prima di chiamare SaveChanges. Molto probabilmente questo valore non esiste nella tabella Schoolclasses.
In altre parole, questo errore non è dovuto a EF 6 vs EF 7 ma a causa di una chiave esterna non trovata nella tabella correlata.