EF - 增删改查

新增

  添加命名空间引用 System.Data.Entity.Infrastructure:

        #region 新增
        static int Add()
        {
            using (NorthwindEntities db = new NorthwindEntities())
            {
                Customers cus = new Customers
                {
                    CustomerID = "zouqj",
                    Address = "南山区新能源创新产业园",
                    City = "深圳",
                    Phone = "15243641131",
                    CompanyName = "深圳跨境翼电商商务有限公司",
                    ContactName = "邹琼俊"
                };

                //方法一
                //db.Customers.Add(cus);

                //方法二(还是方法一简单一些)
                DbEntityEntry<Customers> entry = db.Entry(cus);
                entry.State = System.Data.Entity.EntityState.Added;

                return db.SaveChanges();
            }
        }
        #endregion

查询

  简单查询和延时加载

猜你喜欢

转载自www.cnblogs.com/zhangchaoran/p/8862846.html