public void Add2() { TblStudent stu = new TblStudent(); stu.FirstName = "Shadow"; stu.LastName = "Sun"; stu.Age = 20; if (db.TblStudent.Where <TblStudent>(s => (s.FirstName + s.LastName) == (stu.FirstName + stu.LastName)).Count() > 0) { Console.WriteLine($"重复姓名:{stu.FirstName} {stu.LastName}"); } else { db.TblStudent.Add(stu); db.SaveChanges(); Console.WriteLine($"新增学生:{stu.FirstName} {stu.LastName}"); } }
public void Add() { TblStudent stu = new TblStudent(); stu.FirstName = "Shadow"; stu.LastName = "Moon"; stu.Age = 20; if (db.TblStudent.Where <TblStudent>(s => (s.FirstName + s.LastName) == (stu.FirstName + stu.LastName)).Count() > 0) { Console.WriteLine($"重复姓名:{stu.FirstName} {stu.LastName}"); } else { //方法一: //sde.TblStudent.Add(stu); //方法二: db.Entry(stu).State = System.Data.Entity.EntityState.Added; db.SaveChanges(); Console.WriteLine($"新增学生:{stu.FirstName} {stu.LastName}"); } }