Пример #1
0
 private static void Main()
 {
     using (var taskMonitorContext = new TaskMonitorContext())
     {
         var taskCategory = new TaskCategory {
             CategoryName = "Meeting", CreatedUtc = DateTime.UtcNow
         };
         var user = new User {
             UserName = "******", Tasks = new List <Task>(), CreatedUtc = DateTime.UtcNow
         };
         var task = new Task
         {
             Category    = taskCategory,
             StartDate   = DateTime.UtcNow,
             EndDate     = DateTime.UtcNow.AddDays(2).AddHours(3).AddMinutes(30),
             Description = "This is a sample task!",
             User        = user,
             CreatedUtc  = DateTime.UtcNow
         };
         user.Tasks.Add(task);
         taskMonitorContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);
         var transaction = taskMonitorContext.Database.CurrentTransaction;
         taskMonitorContext.Tasks.Add(task);
         taskMonitorContext.TaskCategories.Add(taskCategory);
         taskMonitorContext.Users.Add(user);
         taskMonitorContext.SaveChanges();
         transaction.Rollback();
     }
 }
        /// <summary>
        /// Posts the specified new task.
        /// </summary>
        /// <param name="newTaskViewModel">The new task.</param>
        /// <param name="userName">Name of the user.</param>
        /// <returns>
        /// Created task.
        /// </returns>
        public Task CreateTask(NewTaskViewModel newTaskViewModel, string userName)
        {
            var task = new Task
            {
                UserName    = userName,
                CreatedDate = DateTime.UtcNow,
                Description = newTaskViewModel.Description
            };

            if (null != newTaskViewModel.StartDate)
            {
                task.StartDate = newTaskViewModel.StartDate.Value.ToUniversalTime();
            }

            if (null != newTaskViewModel.EndDate)
            {
                task.EndDate = newTaskViewModel.EndDate.Value.ToUniversalTime();
            }

            taskMonitorContext.Tasks.Add(task);
            taskMonitorContext.SaveChanges();
            return(task);
        }