时间:2021-07-01 10:21:17 帮助过:20人阅读
1.查询操作
var uesrs = from a in db
where a.id == 1
select a;
users.First(); //返回第一个,如果没有数据,出异常
users.FirstOrDefault(); //返回第一个或者null
users.SingleOrDefault();// 返回第一个或者null,如果有多个数据出异常
2.删除操作
(1)
var uesrs = from a in db
where a.id == 1
select a;
UserInfo user = users.FirstOrDefault();
db.UserInfos.Remove(user);
context.SaveChanges();
(2)
var uesrs = from a in db
where a.id == 1
select a;
UserInfo user = users.FirstOrDefault();
db.Entry<UserInfo>(user).State = System.Data.EntityState.Deleted;
db.SaveChanges();
(3)
UserInfo user = new UserInfo(){Id=1}; //主键
db.Entry<UserInfo>(user).State = System.Data.EntityState.Deleted; //先找到改对象,在打上删除标记
db.SaveChange();
3.更新操作(Modify)
var uesrs = from a in db
where a.id == 1
select a;
UserInfo user = users.FirstOrDefault();
user.Name="admin";
db.Entry<UserInfo>(user).State = System.Data.EntityState.Modify;
db.SaveChanges();
数据库操作
标签:sys try 标记 没有 操作 odi 个数 nbsp rem