نوشته شده توسط
mohamad_torabi
سلام خیلی ممنون میشه بگید چطور میشه جدول رو اپدیت کرد؟
به نظر میاد این خطا برا اینه که رکورد در جدول Test وجود داره.!!!
چند راه برا Update وجود داره .
اول :
try
{
var _test = db.Test.FirstOrDefault(x=>x.NAME=="ali");
if (_test != null)
{
_test.NAME = textBox6.Text,
_test.FAMILY = textBox7.Text,
_test.Lesson = textBox5.Text,
_test.Section = textBox4.Text,
_test.MODIR = textBox3.Text,
_test.YEAR = textBox2.Text,
_test.NAME_SCHOOL = textBox1.Text,
_test.pic = textBox8.Text
db.Test.Attach(_test);
db.Entry(_test).State = EntityState.Modified;
db.SaveChanges();
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
دوم :
try
{
var _test = db.Test.FirstOrDefault(x=>x.NAME=="ali");
if (_test != null)
{
_test.NAME = textBox6.Text,
_test.FAMILY = textBox7.Text,
_test.Lesson = textBox5.Text,
_test.Section = textBox4.Text,
_test.MODIR = textBox3.Text,
_test.YEAR = textBox2.Text,
_test.NAME_SCHOOL = textBox1.Text,
_test.pic = textBox8.Text
db.Entry(_test).CurrentValues.SetValues(_test);
db.SaveChanges();
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
ضمنا برا جدولات فیلد IDentity بزار.
فرضا من خواستم فردی که نامش "ali" هست را ویرایش کنم.