需要用到的数据库:
新增代码:
ClassInfo classInfo = new ClassInfo() {
Name="7",
Remark="芜湖",
TeacherID=1
};
db.ClassInfos.Add(classInfo);
db.SaveChanges();
批量新增:
List<ClassInfo> classInfos = new List<ClassInfo>()
{
new ClassInfo()
{
Name="9",
Remark="芜湖",
TeacherID=1
},
new ClassInfo()
{
Name="10",
Remark="芜湖",
TeacherID=1
}
};
db.Entry(classInfos).State = System.Data.Entity.EntityState.Added;
db.SaveChanges();
删除:
var b = db.ClassInfos.Find(1);
db.ClassInfos.Remove(b);
db.SaveChanges();
批量删除:
var c = db.ClassInfos.Where(p => p.Remark == "芜湖").ToList();
db.ClassInfos.RemoveRange(c);
db.SaveChanges();
修改:
var a = db.ClassInfos.Find(1);
a.Name = "";
查询:
//精确查询
var e = db.ClassInfos.Where(p => p.Name == "").ToList();
//模糊查询
var f = db.ClassInfos.Where(p => p.Name.Contains("")).ToList();
//组合查询:并且
var g = db.ClassInfos.Where(p => p.Name == "" && p.Term == 1).ToList();
//组合查询:或者
var h = db.ClassInfos.Where(p => p.Name == "" || p.Term ==1).ToList();