예제 #1
0
        private static async Task FillAuthorsAsync(BookDataContext context)
        {
            // Note that we are jus reading primary keys of books, not entire
            // book records. Tip: Always read only those columns that you REALLY need.
            var bookIDs = await context.Books.Select(b => b.BookID).ToArrayAsync();

            var authors = JsonSerializer.Deserialize <IEnumerable <Author> >(
                await File.ReadAllTextAsync("Data/Authors.json"));

            using var transaction = context.Database.BeginTransaction();

            var rand = new Random();

            foreach (var author in authors)
            {
                //Authors.jsonから読み込んだAuthorデータから
                //新しくAuthorインスタンスを作る
                //直接authorを使ってはいけない?
                //直接authorを使っても良いけれど,
                //もしauthorが変更された場合,それがDBに登録されることになる.
                //それを避けるためか?
                var dbAuthor = new Author
                {
                    AuthorName  = author.AuthorName,
                    Nationality = author.Nationality
                };

                // Randomly assign each author one book.
                // Note that we can use the dbAuthor, although we have not yet written
                // it to the database. Also note that we are using the book ID as a
                // foreign key.
                // 最後に,AuthorをDBに追加するときは
                // Bookとの多対多の関係を登録する必要がある
                // ここはちょっとくせがあるな.
                // AuthorオブジェクトとBookIDを使う.
                // AuthorとBookじゃないんだな.
                var dbBookAuthor = new BookAuthor
                {
                    Author = dbAuthor,
                    BookID = bookIDs[rand.Next(bookIDs.Length)]
                };

                //ここまででまだcontext.Authors.Addが出てきていないことに注目!

                // Note that we do NOT need to add dbAuthor. It is referenced by
                // dbBookAuthor, that is enough.
                // BookAuthorsにAddするだけよい.
                // これだけでAuthorsテーブルにもAuthorが登録される.
                // これはEntityFrameworkがやってくれる.
                context.BookAuthors.Add(dbBookAuthor);
            }

            await context.SaveChangesAsync();

            await transaction.CommitAsync();
        }
예제 #2
0
        private static async Task FillAuthorsAsync(BookDataContext context)
        {
            // Note that we are jus reading primary keys of books, not entire
            // book records. Tip: Always read only those columns that you REALLY need.
            var bookIDs = await context.Books.Select(b => b.BookID).ToArrayAsync();

            var authors = JsonSerializer.Deserialize <IEnumerable <Author> >(
                await File.ReadAllTextAsync("Data/Authors.json"));

            using var transaction = context.Database.BeginTransaction();

            var rand = new Random();

            foreach (var author in authors)
            {
                var dbAuthor = new Author
                {
                    AuthorName  = author.AuthorName,
                    Nationality = author.Nationality
                };

                // Randomly assign each author one book.
                // Note that we can use the dbAuthor, although we have not yet written
                // it to the database. Also note that we are using the book ID as a
                // foreign key.
                var dbBookAuthor = new BookAuthor
                {
                    Author = dbAuthor,
                    BookID = bookIDs[rand.Next(bookIDs.Length)]
                };

                // Note that we do NOT need to add dbAuthor. It is referenced by
                // dbBookAuthor, that is enough.
                context.BookAuthors.Add(dbBookAuthor);
            }

            await context.SaveChangesAsync();

            await transaction.CommitAsync();
        }