public IActionResult Index() { if (myDBContext.Authers.Count() == 0) { myDBContext.Add(new Book() { Name = "三国演义", Content = "三国", AutherID = 2, DateTime = DateTime.Now }); myDBContext.Add(new Auther() { //AutherID = 1, Name = "齐白石" }); } //myDBContext.SaveChanges(); var modelDto = myDBContext.Books.Join( myDBContext.Authers, e1 => e1.AutherID, e2 => e2.AutherID, (e1, e2) => new { e1, e2 } ).Select(s => new { s.e1.BookID, s.e1.Content, AuthorName = s.e2.Name, }).ToList(); var modelDto2 = from a in myDBContext.Books join b in myDBContext.Authers on a.AutherID equals b.AutherID select new { a.BookID, a.Content, a.Name, AuthorName = b.Name } into c group c by new { c.AuthorName } into d //按照作家分组 select d; ViewBag.Message = "OK!"; var list = myDBContext.Books.AsNoTracking(); //只进行查询 ViewBag.Data = list.ToList(); return(View(list)); }
public void AddBook(Book book) { _db.Add(book); _db.SaveChanges(); }