Ne arayalım?

ARAMIZA KATILIN

BİZE ULAŞIN

Adres:

E-posta:

host/bin/bilisimlife.dll

iletisim@bilisimlife.net




 
Rserit
Developer
       
 1026  
 278

The changes to the database were committed successfully, but an error occurred while updating the object context Hatası

Merhaba,
Ilişkili tablolarda DML işlemlerini yaparken ilişkili tabloya girdiğiniz verinin karşılığı yoksa bu hatayı almanız normaldir.

Hatayı şöyle verir:


The changes to the database were committed successfully,
but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state.  Inner exception message: A referential integrity constraint violation occurred: The prop...


Diyelim ki foreach'te dönerek insert yapıyorsunuz. Birinci kez dönebilir, ikinci kez dönmeyebilir. Böyle bir hata verebilir. Ikinci tabloya kayıt yapılacak ama birinci tabloda karşılığı yok. InnerException kısmında hata şöyle detaylandırılır:


InnerException = {"A referential integrity constraint violation occurred: The property value(s)
 of 'Anatablonuz.Id' on one end of a relationship do not match the property value(s) of 'IliskiliTablo.AnatabloId' on the other end."}


Bu durumu almanızın sebebi ayrı ayrı Add yapıp iki SaveChanges yaptırıyor olabilir misiniz?
Şöyle yazmışsanız hata alırsınız:


DBContext.Anatablo.Add(Deger);
DBContext.SaveChanges();

// Ardından ilişkili tabloma değerleri aktarıyorum

DBContext.UsersIliskiliTablo.Add(IliskiliDeger);
DBContext.SaveChanges();


Bir kere Save yaptırdığınızda sorun olmayacaktır. Yazım şöyle:


DBContext.Anatablo.Add(Deger);
DBContext.UsersIliskiliTablo.Add(IliskiliDeger);
DBContext.SaveChanges();

Şöyle de yazılabilir:

Deger.UsersIliskiliTablo.Add(IliskiliDeger);
DBContext.SaveChanges();


Eğer yine bir hata geliyorsa veritabanına bir kayıt ekleyin. Ondan sonra döngüyle dönün.

Bu şekilde çözmüştüm şahsen.

Kolay gelsin,
Recep.

Developer.