2016-11-01 21 views
9

'da güncellemede farklı şekilde çalışır. EF 6 ile bu yöntemi çalıştırdığımda öğrenci güncellendi!EntityState.Modified, EF Core ile EF 6

public async Task Update(Student student) 
     { 
      context.Entry(student).State = EntityState.Modified; 
      await context.SaveChangesAsync(); 
     } 

Ben EF 7 şey bu yöntemi çalıştırmak veritabanında değişti!

Neyi hatalıyım? Bunu güncellemek için önce varlığı almak istemiyorum!

GÜNCELLEME

Ben SaveChanges etrafında bir try/catch koymak ve bu hata mesajını aldım: Ben modifiye etmek BÜTÜN taraflı bir devlet ayarlamak zaman, bir zaman tek bir sorun var mı

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. 

özellikleri (örneğin Student.SchoolclassId) yabancı bir anahtardır?

GÜNCELLEME Güncelleme yöntemi öğrenci Ad/Soyad özellikleri yeni değerlere sahip girildiğinde 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; } 
} 

!

+0

EF Core'da, EF'de olduğu gibi çalışır. SQL'in çalıştırıldığını görmek için bir profil oluşturucu kullanın. –

+0

Komik ... bu savechanges benim sql profilerimde hiç görünmez! – Pascal

+0

Tabii diğer şeyler benim sql profilimde gösteriyor, bu sorun değil ... – Pascal

cevap

0

Hatanız, Student SchoolclassId öğesinin olduğunu ve bu sınıfın Schoolclasses tablosundaki Kimlik listesinde bulunmadığını bildiren bir hatadır.

SaveChanges'i çağırmadan önce SchoolclassId değerini kontrol edin. Büyük olasılıkla bu değer Schoolclasses tablosunda mevcut değildir. Başka bir deyişle, bu hata EF 6 ile EF 7 arasında değil, ancak ilgili tabloda bulunmayan yabancı anahtar nedeniyle ortaya çıkmaktadır.

+0

Sonunda birisi yorumumu kopyala/yapıştır! ;-) – Pascal