others - C# - 使用 Entity Framework 删除记录时出错

94 5

我尝试使用Entity Framework从数据库中删除记录,但是得到了以下错误:

System.InvalidOperationException :'Attaching an entity of type'PayRoll_v1.database.Administration'failed because another entity of the same type already has the same primary key value .

这是相关的代码:


 var genderCodeAsInt = Convert.ToInt32(txtCode.Text);



 var C = new database.Administration


 {


 Code = genderCodeAsInt,


 };



 db.Administrations.Attach(C);


 db.Administrations.Remove(C);


 db.SaveChanges();



时间: 原作者:

65 5

你可以将条目的附加状态设置为Deleted


db.Administrations.Entry(C).State = EntityState.Deleted;


db.Administrations.SaveChanges();



原作者:
...