我需要使用ado.net实体数据模型删除一行。已经在谷歌上搜索,但我仍然无法找到正确的方法。
这是我的代码:
else if (mode == 3)
{
LaundryEntities1 db = new LaundryEntities1();
var query = (from user in db.Users
where user.UserID == textBoxID.Text
select user).First();
db.DeleteObject(query);
db.SaveChanges();
reload();
MessageBox.Show("Succesfully delete a user");
clear();
}
答案 0 :(得分:0)
您可以使用
LaundryEntities1 db = new LaundryEntities1();
var query = (from user in db.Users
where user.UserID == textBoxID.Text
select user).First();
db.Entry(employer).State = EntityState.Deleted
if(db.SaveChanges())
MessageBox.Show("Succesfully delete a user");
答案 1 :(得分:0)
发现我解决了我自己的问题,我不知道它为什么会起作用lol。 我创建了一个名为Session
的公共类public class session
{
public static DatabaseEntities db = new DatabaseEntities();
public static User user = null;
}
并将我的代码更改为
Users user = (from x in session.db.Users
where x.UserID == textBoxID.Text
select x).FirstOrDefault();
session.db.Users.DeleteObject(user);
session.db.SaveChanges();