前言:
数据上下文: NorthwindContext,
数据成员对象:
public class Province(){...}
1,新增
Province province = new Province { ProvinceID = Guid.NewGuid(), ProvinceNo = "100000", ProvinceName = "北京" }; using (NorthwindContext db = new NorthwindContext()) { db.Provinces.Add(province); db.SaveChanges(); }2,修改
Province province = new Province { ProvinceID = new Guid("F0427980-6CB6-4D5B-91F5-ADE3E676DEA1"), ProvinceNo = "100000", ProvinceName = "上海" }; using (NorthwindContext db = new NorthwindContext()) { db.Entry(province).State = EntityState.Modified; db.SaveChanges(); }3,删除
Province province = new Province { ProvinceID = new Guid("F0427980-6CB6-4D5B-91F5-ADE3E676DEA1") }; using (NorthwindContext db = new NorthwindContext()) { db.Provinces.Attach(province); db.Provinces.Remove(province); db.SaveChanges(); }4,查询
using (NorthwindContext db = new NorthwindContext()) { var expr = from p in db.Provinces select p; foreach (var item in expr) { Console.WriteLine("{0}-{1}-{2}", item.ProvinceID, item.ProvinceNo, item.ProvinceName); } }
原文地址:http://blog.csdn.net/rj532029887/article/details/42079519