testEntities db=new testEntities();
增:
UserInfo userinfo = new UserInfo()
{
UserName = "小王",
Sex = "男"
};
db.UserInfo.Add(userinfo);
db.SaveChanges();
删:
方法一:先查询出要删除的数据
var DeleteData= db.UserInfo.Where(u => u.Name ==”张三”).FirstOrDefault();
db.UserInfo.Remove(DeleteData);
db.SaveChanges();
方法二:根据主键进行删除
UserInfo userInfo= new UserInfo() { Id = 1 };
db.Users.Attach(userInfo);
db.Users.Remove(userInfo);
db.SaveChanges();
方法三:
UserInfo userinfo = new UserInfo()
{
Id = 1
};
db.Entry<UserInfo>(userinfo).State = System.Data.Entity.EntityState.Deleted;
db.SaveChanges();
改:
方法一:先查询后修改
var userInfo = db.UserInfo.Where(u => u.Id == 1).FirstOrDefault();
userInfo.UserName = "李四";
userInfo.Sex="女";
db.SaveChanges();
方法二:
UserInfo userinfo = new UserInfo()
{
Id = 1,
UserName = "李四",
Sex = "女"
};
db.Entry<UserInfo>(user).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();