public ActionResult Register(User user) { try { if (ModelState.IsValid) { using (OpsIntel1Entities1 context = new OpsIntel1Entities1()) { User newUser = new User(); // var newUser = context.Users.Create(); newUser.Email = user.Email; newUser.Password = user.Password; newUser.Username = user.Username; newUser.FirstName = user.FirstName; newUser.LastName = user.LastName; newUser.CreatedDate = DateTime.Now; context.Users.Add(newUser); context.SaveChanges(); return RedirectToAction("LogIn", "Home"); } } else { ModelState.AddModelError("", "Data is not correct"); } } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return View(); }
public bool AddVMInDB(VMDetails VMData, string BlobLocation, List<SummaryUtil> UtilPattern) { using (OpsIntel1Entities1 context = new OpsIntel1Entities1()) { VMThreshold details = new VMThreshold { VMName = VMData.Name, Location = VMData.Location, Description = VMData.Description, UtilFileLocation = VMData.UtilFileLocation, Status = "Created" }; context.VMThresholds.Add(details); context.SaveChanges(); var id = details.Id; foreach (var u in UtilPattern) { // Console.WriteLine(u.UtilizationType); foreach (var tr in u.TimeRanges) { //string InsertUtilPatternQuery = "INSERT into VMAutomationRules (VMID, UtilizationType, StartTime, EndTime) "; //InsertUtilPatternQuery += " VALUES(@VMID, @UtilizationType, @StartTime, @EndTime);"; //SqlCommand InsertUtilData = new SqlCommand(InsertUtilPatternQuery, conn); double Start = tr.StartTime ?? 0; double Stop = tr.StopTime ?? 0; TimeSpan StartTime = TimeSpan.FromHours(Start); TimeSpan StopTime = TimeSpan.FromHours(Stop); VMAutomationRule InsertUtilData = new VMAutomationRule { VMID = id, UtilizationType = u.UtilizationType, StartTime = StartTime, EndTime = StopTime }; context.VMAutomationRules.Add(InsertUtilData); context.SaveChanges(); //InsertUtilData.Parameters.AddWithValue("@VMID", id); //InsertUtilData.Parameters.AddWithValue("@UtilizationType", u.UtilizationType); //InsertUtilData.Parameters.AddWithValue("@StartTime", StartTime); //InsertUtilData.Parameters.AddWithValue("@EndTime", StopTime); //var res = InsertUtilData.ExecuteNonQuery(); //Console.WriteLine("{0}, {1}", tr.StartTime, tr.StopTime); } } } //using (var conn = new SqlConnection(ConnString)) //{ // conn.Open(); // string InsertVMQuery = "Insert into VMThreshold (VMName, CloudServiceName, Description, UtilFileLocation, Status)"; // InsertVMQuery += "values (@VMName, @Location, @Description, @UtilFileLocation, @Status); select SCOPE_IDENTITY();"; // //InsertVMQuery += "Select @Id = Scope_Identity()"; // SqlCommand InsertVMCommand = new SqlCommand(InsertVMQuery, conn); // InsertVMCommand.Parameters.AddWithValue("@VMName", VMData.Name); // InsertVMCommand.Parameters.AddWithValue("@Location", VMData.Location); // InsertVMCommand.Parameters.AddWithValue("@Description", VMData.Description); // InsertVMCommand.Parameters.AddWithValue("@UtilFileLocation", BlobLocation); // InsertVMCommand.Parameters.AddWithValue("@Status", "Created"); // //myCommand.Parameters.Add("@Id", SqlDbType.Int); // var id = InsertVMCommand.ExecuteScalar(); // foreach (var u in UtilPattern) // { // Console.WriteLine(u.UtilizationType); // foreach (var tr in u.TimeRanges) // { // string InsertUtilPatternQuery = "INSERT into VMAutomationRules (VMID, UtilizationType, StartTime, EndTime) "; // InsertUtilPatternQuery += " VALUES(@VMID, @UtilizationType, @StartTime, @EndTime);"; // SqlCommand InsertUtilData = new SqlCommand(InsertUtilPatternQuery, conn); // double Start = tr.StartTime ?? 0; // double Stop = tr.StopTime ?? 0; // TimeSpan StartTime = TimeSpan.FromHours(Start); // TimeSpan StopTime = TimeSpan.FromHours(Stop); // InsertUtilData.Parameters.AddWithValue("@VMID", id); // InsertUtilData.Parameters.AddWithValue("@UtilizationType", u.UtilizationType); // InsertUtilData.Parameters.AddWithValue("@StartTime", StartTime); // InsertUtilData.Parameters.AddWithValue("@EndTime", StopTime); // var res = InsertUtilData.ExecuteNonQuery(); // Console.WriteLine("{0}, {1}", tr.StartTime, tr.StopTime); // } // } //} return true; }