I get the following error when I add more than one row: The property 'Id' is part of the object's key information and cannot be modified. multirow
if (tblCari.TblCariAdres.Count != 0) {
TblCariAdres tblCariAdres = new TblCariAdres();
foreach (var item in tblCari.TblCariAdres) {
tblCariAdres.Id = Guid.NewGuid();
tblCariAdres.FirmaId = tblCari1.FirmaId;
tblCariAdres.CariId = tblCari1.Id;
tblCariAdres.Tanim = item.Tanim;
tblCariAdres.Isim = item.Isim;
tblCariAdres.Adres = item.Adres;
tblCariAdres.Ilce = item.Ilce;
tblCariAdres.Sehir = item.Sehir;
tblCariAdres.Telefon = item.Telefon;
tblCariAdres.Aciklama = item.Aciklama;
Db.TblCariAdres.Add(tblCariAdres);
Db.SaveChanges();
}
}
You are trying to add the same variable to the database multiple times. As such your database submission (Db.SaveChanges()
) is trying to change the Id
(and other properties) of the existing entity on the second loop.
Try moving this line into the loop: TblCariAdres tblCariAdres = new TblCariAdres();
Such that your code looks like the below:
if (tblCari.TblCariAdres.Count != 0) {
foreach (var item in tblCari.TblCariAdres) {
TblCariAdres tblCariAdres = new TblCariAdres();
tblCariAdres.Id = Guid.NewGuid();
tblCariAdres.FirmaId = tblCari1.FirmaId;
tblCariAdres.CariId = tblCari1.Id;
tblCariAdres.Tanim = item.Tanim;
tblCariAdres.Isim = item.Isim;
tblCariAdres.Adres = item.Adres;
tblCariAdres.Ilce = item.Ilce;
tblCariAdres.Sehir = item.Sehir;
tblCariAdres.Telefon = item.Telefon;
tblCariAdres.Aciklama = item.Aciklama;
Db.TblCariAdres.Add(tblCariAdres);
Db.SaveChanges();
}
}