HBS.Data.Entities.SchedulingTimeTracking.Models.UserTimeTrackHistoryMapped.Save C# (CSharp) Method

Save() public method

public Save ( ) : int
return int
        public int Save()
        {
            using (var dbContext = new SchTimeTrackingEntities())
            {
                UserName = dbContext.UserProfiles.FirstOrDefault(c => c.UserId.Equals(UserId)).UserName;
                var utth = new UserTimeTrackHistory
                    {
                        UserId = UserId,
                        UserName = UserName,
                        ClockInTime = ClockInTime,
                        ClockOutTime = ClockOutTime,
                        StampDate = StampDate,
                        UserIP = UserIp,
                        IsDeleted = IsDeleted,
                        CreatedBy = CreatedBy,
                        CreatedDate = CreatedDate
                    };
                dbContext.UserTimeTrackHistories.Add(utth);
                dbContext.SaveChanges();
                return utth.TimeTrackId;
            }
        }

Usage Example

        public ActionResult CreateTimeTrack(FormCollection collection)
        {
            UserTimeTrackHistoryMapped utth = new UserTimeTrackHistoryMapped(getCompanyId());
            //var userList = MembershipUserExtended.GetFullNameUserNameList();
            string userName = collection["UserName"];
            utth.UserName = userName;

            DateTime clockInDt;
            DateTime clockOutDt;
            if (DateTime.TryParse(collection["ClockInTime"], out clockInDt) && DateTime.TryParse(collection["ClockOutTime"], out clockOutDt))
            {
                if (clockOutDt.TimeOfDay.CompareTo(clockInDt.TimeOfDay) != -1) // if clock out time is earlier than clock in time than error
                {
                    utth.ClockInTime = string.Format("{0:t}", clockInDt);
                    utth.ClockOutTime = string.Format("{0:t}", clockOutDt);
                    utth.UserId = Convert.ToInt32(userName);
                    //utth.UserId = ((HBS.WebPortal.Models.User)Session["user"]).userid;
                    utth.CreatedBy = ((HBS.WebPortal.Models.User)Session["user"]).UserName;
                    utth.CreatedDate = HBS.Data.Entities.SchedulingTimeTracking.Infrastructure.WebHelpers.GetCurrentDateTimeByTimeZoneId(System.Configuration.ConfigurationManager.AppSettings["UserTimeZoneId"]);
                    utth.IsDeleted = false;
                    var tth = utth.Get(utth.Save(),getCompanyId());
                    tth.UserName = userName;
                    ViewBag.Message = "Record inserted successfully.";
                    return View(tth);
                }
                else
                {
                    ViewBag.Message = "Clock Out time can not be earlier than Clock In time.";
                    return View(utth);
                }
            }
            else
            {
                ViewBag.Message = "Not a valid Clock In/Out time, please make sure time is in correct format.";
                return View(utth);
            }
            //ViewBag.Message = "Error inserting record.";
            //return View(new UserTimeTrackHistoryMapped());
        }