public ActionResult addGuestbook(GuestbookViewModels model)
        {
            model.createdate = DateTime.Now;
            model.ip         = Request.UserHostAddress;
            //AutoMapper自动映射
            Mapper.Initialize(cfg => cfg.CreateMap <GuestbookViewModels, Guestbook> ());
            Guestbook   models      = Mapper.Map <GuestbookViewModels, Guestbook> (model);
            BlogArticle blogArticle = BlogArticleServive.QueryWhere(a => a.bID == model.blogId).FirstOrDefault();

            blogArticle.bcommentNum += 1;
            BlogArticleServive.SaverChanges();
            GuestbookServices.Add(models);
            GuestbookServices.SaverChanges();
            ViewBag.gblist = GuestbookServices.QueryOrderBy(c => c.blogId == model.blogId, c => c.createdate, false).ToPagedList(1, 5);
            return(PartialView("_GuestbookPage"));
        }
        public async Task <MessageModel <string> > Post([FromBody] Guestbook request)
        {
            var data = new MessageModel <string>();

            var id = await _guestbookServices.Add(request);

            data.success = id > 0;

            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg      = "添加成功";
            }

            return(data);
        }
        public async Task <IEnumerable <string> > Get()
        {
            try
            {
                Console.WriteLine($"");
                Console.WriteLine($"Begin Transaction");
                _unitOfWork.BeginTran();
                Console.WriteLine($"");
                var passwords = await _passwordLibServices.Query(d => d.IsDeleted == false);

                Console.WriteLine($"first time : the count of passwords is :{passwords.Count}");


                Console.WriteLine($"insert a data into the table PasswordLib now.");
                var insertPassword = await _passwordLibServices.Add(new PasswordLib()
                {
                    IsDeleted     = false,
                    plAccountName = "aaa",
                    plCreateTime  = DateTime.Now
                });


                passwords = await _passwordLibServices.Query(d => d.IsDeleted == false);

                Console.WriteLine($"second time : the count of passwords is :{passwords.Count}");

                //......

                Console.WriteLine($"");
                var guestbooks = await _guestbookServices.Query();

                Console.WriteLine($"first time : the count of guestbooks is :{guestbooks.Count}");

                int ex = 0;
                Console.WriteLine($"\nThere's an exception!!");
                int throwEx = 1 / ex;

                Console.WriteLine($"insert a data into the table Guestbook now.");
                var insertGuestbook = await _guestbookServices.Add(new Guestbook()
                {
                    username   = "******",
                    blogId     = 1,
                    createdate = DateTime.Now,
                    isshow     = true
                });

                guestbooks = await _guestbookServices.Query();

                Console.WriteLine($"second time : the count of guestbooks is :{guestbooks.Count}");

                _unitOfWork.CommitTran();
            }
            catch (Exception)
            {
                _unitOfWork.RollbackTran();
                var passwords = await _passwordLibServices.Query();

                Console.WriteLine($"third time : the count of passwords is :{passwords.Count}");

                var guestbooks = await _guestbookServices.Query();

                Console.WriteLine($"third time : the count of guestbooks is :{guestbooks.Count}");
            }

            return(new string[] { "value1", "value2" });
        }
Example #4
0
        public async Task <MessageModel <IEnumerable <string> > > Get()
        {
            List <string> returnMsg = new List <string>()
            {
            };

            try
            {
                returnMsg.Add($"Begin Transaction");

                _unitOfWork.BeginTran();
                var passwords = await _passwordLibServices.Query(d => d.IsDeleted == false);

                returnMsg.Add($"first time : the count of passwords is :{passwords.Count}");


                returnMsg.Add($"insert a data into the table PasswordLib now.");
                var insertPassword = await _passwordLibServices.Add(new PasswordLib()
                {
                    IsDeleted     = false,
                    plAccountName = "aaa",
                    plCreateTime  = DateTime.Now
                });


                passwords = await _passwordLibServices.Query(d => d.IsDeleted == false);

                returnMsg.Add($"second time : the count of passwords is :{passwords.Count}");
                returnMsg.Add($" ");

                //......

                var guestbooks = await _guestbookServices.Query();

                returnMsg.Add($"first time : the count of guestbooks is :{guestbooks.Count}");

                int ex = 0;
                returnMsg.Add($"There's an exception!!");
                returnMsg.Add($" ");
                int throwEx = 1 / ex;

                var insertGuestbook = await _guestbookServices.Add(new Guestbook()
                {
                    username   = "******",
                    blogId     = 1,
                    createdate = DateTime.Now,
                    isshow     = true
                });

                guestbooks = await _guestbookServices.Query();

                returnMsg.Add($"first time : the count of guestbooks is :{guestbooks.Count}");
                returnMsg.Add($" ");

                _unitOfWork.CommitTran();
            }
            catch (Exception)
            {
                _unitOfWork.RollbackTran();
                var passwords = await _passwordLibServices.Query();

                returnMsg.Add($"third time : the count of passwords is :{passwords.Count}");

                var guestbooks = await _guestbookServices.Query();

                returnMsg.Add($"third time : the count of guestbooks is :{guestbooks.Count}");
            }

            return(new MessageModel <IEnumerable <string> >()
            {
                success = true,
                msg = "操作完成",
                response = returnMsg
            });
        }