增+查
using EFDemo01.Modules; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.Entity.Validation; namespace EFDemo01 { internal class Program { static void Main(string[] args) { using (var db = new DBModel()) { /** */ //person表insert数据 db.people.Add(new person { name = "张三", age = 20, position = "码农", departmentId = 2 }); db.people.Add(new person { name = "李四", age = 19, position = "工程", departmentId = 3 }); //department表insert数据 db.departments.Add(new department { departmentName = "SI" }); db.departments.Add(new department { departmentName = "CIM" }); db.departments.Add(new department { departmentName = "TED" }); //提交transact sql db.SaveChanges(); //查 var resultPeople = db.people.Where(p => p.age >= 20).DefaultIfEmpty(); foreach (var per in resultPeople) { Console.WriteLine($"姓名:{per.name}, 年龄:{per.age}, 部门:{per.position}"); } var resultDepartment = from depar in db.departments select depar; foreach (var depar in resultDepartment) { Console.WriteLine($"部门:{depar.departmentName}"); } } Console.Read(); } } }
改
using EFDemo01.Modules; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.Entity.Validation; namespace EFDemo01 { internal class Program { static void Main(string[] args) { using (var db = new DBModel()) { var resultBeforeUpdate = db.people.Where(p => p.age >= 20).DefaultIfEmpty(); //改 var person = db.people.Where(p => p.name=="小明").FirstOrDefault(); if (person != null) { person.age = 22; db.SaveChanges(); } var resultAfterUpdate = db.people.Where(p => p.age >= 20).DefaultIfEmpty(); Console.WriteLine("修改前:"); foreach (var per in resultBeforeUpdate) { Console.WriteLine($"姓名:{per.name}, 年龄:{per.age}, 部门:{per.position}"); } Console.WriteLine("修改后:"); foreach (var per in resultAfterUpdate) { Console.WriteLine($"姓名:{per.name}, 年龄:{per.age}, 部门:{per.position}"); } } Console.Read(); } } }
标签:改查,age,db,System,per,Entity,FrameWork,var,using From: https://www.cnblogs.com/kezhang/p/17393852.html