.NET Entity Framework基本使用方法

來源:互聯網
上載者:User

標籤:img   rom   col   上下   esc   div   net   圖片   amp   

產生模型

 

EF有兩種查詢方式,Linq查詢 、Lambda運算式

            //普通查詢 Linq 方式            IQueryable<Book> list = from b in db.Set<Book>()                       where b.BookId > 5 && b.TypeId == 3                       select b;            //普通查詢 Lambda 方式            IQueryable<Book> list1 = db.Set<Book>()                .Where(b => b.BookId > 5 && b.TypeId == 3);

 

 

            //多表查詢 Linq            var list = from book in db.Set<Book>()                       join bookType in db.Set<BookType>()                       on book.TypeId equals bookType.TypeId                       select new                       {                           book                       };            //多表查詢 lambda            var list = db.Set<Book>()                .Where(u => u.BookId > 2 && u.TypeId == 4)                .Join(db.Set<BookType>(), book => book.TypeId, tp => tp.TypeId, (book, tp) => new { book, tp });

 

            //消極式載入 使用時才查詢資料庫  沒用一次就查一次            IQueryable<Book> list = db.Set<Book>()                .OrderByDescending(u => u.BookId)    //IQueryable對象中Expression方法預設將Lambda運算式轉換成Expression對象                .Skip(2)                .Take(3);            //不具備消極式載入,因為IEnumerable沒有方法去拼接完整sql語句            IEnumerable<Book> list1 = db.Set<Book>()                .AsEnumerable()   //把結果強轉成 IEnumerable 類型                .OrderByDescending(u => u.BookId)                .Skip(2)                .Take(3);

 

增加資料

        [HttpPost]        public ActionResult Add(Book book)        {            DbContext db = new DbModel();            db.Set<Book>().Add(book);            //返回受影響行數            if(db.SaveChanges() > 0)            {                return Json(new                {                    msg = "添加成功"                });            }else            {                return Json(new                {                    msg = "添加失敗"                });            }        }

修改資料

        [HttpPost]        public ActionResult Edit(Book bb) {            DbContext db = new DbModel();            //將對象附加到上下文  狀態設定為未更改            db.Set<Book>().Attach(bb);            db.Entry(bb).State = EntityState.Modified;            db.SaveChanges();            return Json(new            {                msg = "修改成功"            });        } 

刪除資料

        public ActionResult Delete(int id) {            DbContext db = new DbModel();            Book book = db.Set<Book>().FirstOrDefault(b => b.BookId == id);            db.Set<Book>().Remove(book);            db.SaveChanges();            return Json(new            {                msg = "刪除成功"            },JsonRequestBehavior.AllowGet);        }

 

.NET Entity Framework基本使用方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.