none
ENTITY FRAMEWORK如何做事务? RRS feed

  • 问题

  •             int insertCount = rand.Next(10, 50);
                TB_User user = new TB_User();
                TB_UserBook userBook = new TB_UserBook();
                TB_UserBookChapter userBookChapter = new TB_UserBookChapter();
                for (int i = 0; i < insertCount; i++)
                {
                    user.UserAccount = Guid.NewGuid().ToString();
                    user.UserComment = Guid.NewGuid().ToString();
                    user.UserPassword = Guid.NewGuid().ToString();
                    DB.TB_User.Add(user);
                    DB.SaveChanges();

                    for (int j = 0; j < 5; j++)
                    {
                        userBook.UserID = user.UserID;
                        userBook.UserBookName = "book-" + Guid.NewGuid().ToString();
                        userBook.UserBookComment = "comment-" + Guid.NewGuid().ToString();
                        userBook.UserBookPrice = rand.Next(1000, 9999);
                        DB.TB_UserBook.Add(userBook);
                        DB.SaveChanges();
                    }
                }

    tb_user插入成功后,返回自增长ID后再插入5条关联数据。我要全部成功的时候才写入。


    为“金”之计“问”。

    2013年3月23日 15:41

全部回复