private static void AddBooks() { using (var db = new BooksDbContext()) { var author1 = db.Authors.Single(a => a.Name == "애거사 크리스티"); var book1 = new Book { Title = "그리고 아무도 없었다.", PublishedYear = 1939, Author = author1 }; db.Books.Add(book1); var author2 = db.Authors.Single(a => a.Name == "찰스 디킨스"); var book2 = new Book { Title = "두 도시 이야기", PublishedYear = 1859, Author = author2 }; db.Books.Add(book2); db.SaveChanges(); } }
public void DeleteBookScenarioBasic() { var options = new DbContextOptionsBuilder <BooksDbContext>() .UseInMemoryDatabase(databaseName: "DeleteBookScenarioBasic") .Options; // Insert seed data into the database using one instance of the context using (var ctx = new BooksDbContext(options)) { ctx.Database.EnsureCreated(); } // Use a clean instance of the context to run the test using (var ctx = new BooksDbContext(options)) { var repo = new BooksRepository(ctx); repo.DeleteBookAsync(3).Wait(); } // Use a clean instance of the context to run the test using (var ctx = new BooksDbContext(options)) { var repo = new BooksRepository(ctx); var books = repo.RetrieveBooksAsync().Result; Assert.Equal(4, books.Count()); } }
static void InsertBooks() { using (var db = new BooksDbContext()) { var book1 = new Book { Title = "坊ちゃん", PublshedYear = 2003, Author = new Author { Birthday = new DateTime(1867, 2, 9), Gender = "M", Name = "夏目漱石" } }; db.Books.Add(book1); var book2 = new Book { Title = "人間失格", PublshedYear = 1990, Author = new Author { Birthday = new DateTime(1909, 6, 19), Gender = "M", Name = "太宰治" } }; db.Books.Add(book2); db.SaveChanges(); } }
static IEnumerable <Book> GetBooks() { using (var db = new BooksDbContext()) { return(db.Books.Where(book => book.Author.Name.StartsWith("제임스")).ToList()); } }
private static void AddAuthors() { using (var db = new BooksDbContext()) { var author1 = new Author() { BirthDay = new DateTime(1890, 09, 15), Gender = "F", Name = "애거사 크리스티" }; db.Authors.Add(author1); var author2 = new Author() { BirthDay = new DateTime(1812, 02, 07), Gender = "M", Name = "찰스 디킨스" }; db.Authors.Add(author2); db.SaveChanges(); } }
public GetBookCommand( [FromServices] IBookResponseMapper responseMapper, [FromServices] BooksDbContext booksDbContext) { _responseMapper = responseMapper; _bookDbContext = booksDbContext; }
// List 13-5 static void InsertBooks() { using (var db = new BooksDbContext()) { var book1 = new Book { Title = "별의 계승자", PublishedYear = 1977, Author = new Author { Birthday = new DateTime(1941, 06, 27), Gender = "M", Name = "제임스 P. 호건", } }; db.Books.Add(book1); var book2 = new Book { Title = "타임머신", PublishedYear = 1895, Author = new Author { Birthday = new DateTime(1866, 09, 21), Gender = "M", Name = "허버트 조지 웰즈", } }; db.Books.Add(book2); db.SaveChanges(); } }
protected void btnViDu4_Click(object sender, EventArgs e) { // SelectMany: // Khi các thực thể không có quan hệ trong EDM // Sử dụng Where để kết Chủ đề với Sách using (BooksDbContext db = new BooksDbContext()) { int donGia = 100000; var query = db.ChuDes .SelectMany(cd => db.Saches .Where(s => (cd.MaChuDe == s.MaChuDe) && s.GiaBan < donGia && s.NgayCapNhat >= new DateTime(2012, 11, 17)) .Select(s => new { cd.MaChuDe, cd.TenChuDe, s.SachID, s.TenSach, s.GiaBan, s.NgayCapNhat }) ) .OrderBy(k => k.NgayCapNhat); var items = query.ToList(); gvwKetQua.DataSource = items; gvwKetQua.DataBind(); } }
public TestController( ILogger <TestController> logger, BooksDbContext db) { this.logger = logger; this.db = db; }
// List 13-11 private static void UpdateBook() { using (var db = new BooksDbContext()) { var book = db.Books.Single(x => x.Title == "별의 계승자"); book.PublishedYear = 2016; db.SaveChanges(); } }
/// <summary> /// Creats a new instance of <see cref="BookDataSeeder"/> /// </summary> /// <param name="hosting">Used for getting the Json data from the content root of the application</param> /// <param name="context">Used to add the seed data to the database</param> /// <param name="logger">Used for logging</param> /// <param name="bookImageService">Used for seeding <see cref="Book"/> Images</param> public BookDataSeeder(IHostingEnvironment hosting, BooksDbContext context, ILogger <BookDataSeeder> logger, IBookImageService bookImageService) { _hosting = hosting; _context = context; _logger = logger; _bookImageService = bookImageService; }
public UnitOfWork(BooksDbContext dbContext) { if (dbContext == null) { throw new ArgumentNullException("DbContext"); } DbContext = dbContext; }
// List 13-14 static IEnumerable <Book> GetBooks() { using (var db = new BooksDbContext()) { return(db.Books .Where(b => b.PublishedYear > 1900) .Include(nameof(Author)) .ToList()); } }
public PostBookCommand( [FromServices] BooksDbContext context, [FromServices] IDbBookMapper dbBookMapper, [FromServices] IDbAuthorBookMapper dbAuthorBookMapper) { _context = context; _bookMapper = dbBookMapper; _authorBookMapper = dbAuthorBookMapper; }
public BooksController(BooksDbContext booksDbContext) { if (booksDbContext == null) { throw new ArgumentNullException("booksDbContext is null"); } _booksDbContext = booksDbContext; }
static void Main(string[] args) { using (var db = new BooksDbContext()) { // [Column] Entity Framework에서 로그를 출력한다 db.Database.Log = sql => { Debug.Write(sql); }; var count = db.Books.Count(); Console.WriteLine(count); } }
public void DeleteBook(int id) { _context = new BooksDbContext(); Book foundBook = _context.Books.SingleOrDefault <Book>(b => b.BookId == id); if (foundBook != null) { _context.Books.Remove(foundBook); } }
private static void QuerySample04() { using (var db = new BooksDbContext()) { var author = db.Authors .Where(a => a.Books.Count() == db.Authors.Max(x => x.Books.Count())) .First(); Console.WriteLine($"{author.Name} {author.Gender} {author.Birthday}"); } }
public BookService(BooksDbContext db, ICurrentUserService user, IMapper mapper, IBus publisher) { this.db = db; this.user = user; this.mapper = mapper; this.publisher = publisher; }
static void Main(string[] args) { using (var db = new BooksDbContext()) { // p.343「Column: Entity FrameworkでLogを採取する」で示したコード db.Database.Log = sql => { Debug.Write(sql); }; var count = db.Books.Count(); Console.WriteLine(count); } }
public void UpdateBookName(int id, Book book) { _context = new BooksDbContext(); Book foundBook = _context.Books.SingleOrDefault <Book>(b => b.BookId == id); if (foundBook != null) { foundBook.BookName = book.BookName; } _context.SaveChanges(); }
private static void Ex1_1() { using (var db = new BooksDbContext()) { var natsume = db.Authors.Single(a => a.Name == "夏目漱石"); var book1 = new Book { Title = "こころ", PublshedYear = 1991, Author = natsume, }; db.Books.Add(book1); var author2 = db.Authors.Single(a => a.Name == "宮沢賢治"); var book2 = new Book { Title = "注文の多い料理店", PublshedYear = 2000, Author = author2, }; db.Books.Add(book2); db.SaveChanges(); } using (var db = new BooksDbContext()) { var kikuchi = new Author { Birthday = new DateTime(1888, 12, 26), Gender = "M", Name = "菊池寛" }; var book1 = new Book { Title = "真珠夫人", PublshedYear = 2003, Author = kikuchi }; db.Books.Add(book1); var kawabata = new Author { Birthday = new DateTime(1899, 6, 14), Gender = "M", Name = "川端康成" }; var book2 = new Book { Title = "伊豆の踊子", PublshedYear = 2003, Author = kawabata }; db.Books.Add(book2); db.SaveChanges(); } }
public static void Migrate() { var dbConn = File.ReadAllText("DbConnection.txt"); var dbOpts = new DbContextOptionsBuilder().UseSqlServer(dbConn); using (var ctx = new BooksDbContext(dbOpts.Options)) { ctx.Database.Migrate(); } }
/// <summary> /// 書籍タイトル出力 /// </summary> static void ReadBooks() { using (var db = new BooksDbContext()) { List <Book> bookList = db.Books.ToList(); foreach (var item in bookList) { Console.WriteLine(item.Title); } } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, BooksDbContext dbContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } dbContext.Database.EnsureCreated(); app.UseMvc(); }
// List 13-12 private static void DeleteBook() { using (var db = new BooksDbContext()) { var book = db.Books.SingleOrDefault(x => x.Id == 10); if (book != null) { db.Books.Remove(book); db.SaveChanges(); } } }
// '13.10 수준높은 쿼리'에 나온 예제 private static void QuerySample01() { using (var db = new BooksDbContext()) { var authors = db.Authors .Where(a => a.Books.Count() >= 2); foreach (var author in authors) { Console.WriteLine($"{author.Name} {author.Gender} {author.Birthday}"); } } }
public AuthController( BooksDbContext context, UserManager <IdentityUser <Guid> > userManager, IHttpContextAccessor httpContextAccessor, IConfiguration configuration) { _context = context; _userManager = userManager; _httpContextAccessor = httpContextAccessor; _configuration = configuration; }
private static void Exercise1_2() { using (var db = new BooksDbContext()) { foreach (var book in db.Books.OrderBy(b => b.Author.Id)) { Console.WriteLine("{0} {1} {2}({3:yyyy/MM/dd})", book.Title, book.PublshedYear, book.Author.Name, book.Author.Birthday ); } } }
static void ShowMaxLengthTitleBook() { using (var db = new BooksDbContext()) { var len = db.Books.OrderByDescending(item => item.Title.Length).First().Title.Length; var query = db.Books.Where(item => item.Title.Length == len); foreach (var item in query) { Console.WriteLine("len={0}, title={1}", item.Title.Length, item.Title); } } }