码迷,mamicode.com
首页 > 其他好文 > 详细

EF-增删改查简单示例

时间:2014-12-22 16:25:14      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:ef   curd   增删改查   

前言:

数据上下文: 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);
                }
            }

EF-增删改查简单示例

标签:ef   curd   增删改查   

原文地址:http://blog.csdn.net/rj532029887/article/details/42079519

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!