数据库操作
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.Entity.EntityState.Deleted;
db.SaveChanges();
(3)
UserInfo user = new UserInfo(){Id=1}; //主键
db.Entry<UserInfo>(user).State = System.Data.Entity.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.Entity.EntityState.Modifed;
db.SaveChanges();