/// <summary> /// 获取当前数据库上下文:简单工厂获取当前DbContext,实现单个请求之间的DbContext单例 /// </summary> /// <returns></returns> public static ServiceDeskContext GetCurrentContext() { ServiceDeskContext _context = CallContext.GetData("TicketsContext") as ServiceDeskContext; if (_context == null) { _context = new ServiceDeskContext(); CallContext.SetData("TicketsContext", _context); } return _context; }
/// <summary> /// 创建新的Tickets /// </summary> /// <param name="viewmodel"></param> /// <returns></returns> public int Create(TicketCreationModel viewmodel, string Name) { int returnValue = -1; Ticket ticket; //ticket.TicketId // Ensure we have a valid viewModel to work with if (state.IsValid) { DateTime now = DateTime.Now; //string user =this.User.Identity.Name; ticket = new Ticket() { AffectsCustomer = viewmodel.AffectsCustomer, Category = viewmodel.Category, CreatedBy = Name, CreatedDate = now, CurrentStatus = "Active", CurrentStatusDate = now, CurrentStatusSetBy = Name, Details = viewmodel.Details, IsHtml = false, LastUpdateBy = Name, LastUpdateDate = now, Priority = viewmodel.Priority, PublishedToKb = false, TagList = viewmodel.TagList, Title = viewmodel.Title, Type = viewmodel.Type, Owner = viewmodel.OtherOwner ? viewmodel.Owner : Name }; DbTickets.Tickets.Add(ticket); ServiceDeskContext context = new ServiceDeskContext(); DbTickets.SaveChanges(); returnValue = ticket.TicketId; //return View(viewModel); } return returnValue; }