//[ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "TopicID,TopicExplain,TopicScore,TopicType,TopicA,TopicB,TopicC,TopicD,TopicSort,TopicAnswer")] Topic topic) { if (ModelState.IsValid) { db.Topic.Add(topic); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(topic)); }
public ActionResult Register(UserAccount account) { if (ModelState.IsValid) { using (db) { db.userAccount.Add(account); db.SaveChanges(); } ModelState.Clear(); ViewBag.Message = account.Username + " successfully registered."; } return(View()); }
public ActionResult Create(Teacher teacher) { try { db.Teacher.Add(teacher); db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception) { return(Content("<script>alert('请输入正确!');history.go(-1)</script>")); throw; } }
public static LoginInfo Login(string loginName,string password) { LoginInfo loginInfo = null; password = EncryptHelper.Md5(password); loginName = loginName.Trim(); using (AccountEntities accountEntities = new AccountEntities()){ User user = accountEntities.User.FirstOrDefault(x => x.LoginName == loginName && x.Password == password && x.IsActive); if(user != null){ string ip = Fetch.UserIp; loginInfo = accountEntities.LoginInfo.FirstOrDefault(x => x.LoginName == loginName && x.ClientIP == ip); if(loginInfo != null) loginInfo.LastAccessTime = DateTime.Now; else{ loginInfo = new LoginInfo { LastAccessTime = DateTime.Now, LoginToken = Guid.NewGuid(), UserID = user.ID, LoginName = loginName, ClientIP = ip, BusinessPermissionString = "101,102,201,202,301,302,303,304,401,402,403", //暂时默认所有权限 CreateTime = DateTime.Now }; accountEntities.LoginInfo.Add(loginInfo); accountEntities.SaveChanges(); } } } return loginInfo; }
/// <summary> /// Update customer with new email /// </summary> /// <param name="customer">updated customer</param> /// <returns>True on success, false otherwise</returns> public bool Update(Customer customer) { bool flag = false; // update in customers table using (TravelExpertsEntities db = new TravelExpertsEntities()) { // get customer from Customer table by phone number var cust = db.Customers.SingleOrDefault(c => c.CustBusPhone == customer.CustBusPhone); if (cust != null) // found customer { cust.CustEmail = customer.CustEmail; db.SaveChanges(); flag = true; } } // update in accounts table using (AccountEntities db = new AccountEntities()) { // get account var account = db.AspNetUsers.SingleOrDefault(accnt => accnt.PhoneNumber == customer.CustBusPhone); if (account != null) // found account { if (flag) // make sure customers table update succesfully { account.Email = customer.CustEmail; db.SaveChanges(); return(true); } } return(false); // one or both failed } }
public ActionResult BeginAnswer(int PaperID, int?AnswerID) { if (AnswerID == null) { Answer answer = new Answer(); answer.PaperID = PaperID; answer.StuID = (int)Session["LoginID"]; answer.TeaID = 1; answer.AnswerScore = 0; answer.AnswerTime = DateTime.Now; answer.AnswerState = 0; db.Answer.Add(answer); db.SaveChanges(); return(RedirectToAction("AnswerDatail", new { id = answer.AnswerID })); } else { return(RedirectToAction("AnswerDatail", new { id = AnswerID })); } }
/// <summary> /// Update a customer's user name in AspNetUsers table AND Customers table /// </summary> /// <param name="newCustomer">Customer object to update in database</param> /// <returns>True on success, false otherwise</returns> public static bool UpdateAccountUserName(Customer newCustomer) { // update accounts table using (AccountEntities db = new AccountEntities()) { // get account from AspNetUsers table by email var account = db.AspNetUsers.SingleOrDefault(accnt => accnt.Email == newCustomer.CustEmail); if (account != null) // found account { account.UserName = newCustomer.UserName; db.SaveChanges(); return(true); } return(false); } }
public ActionResult Edit(Student student) { db.Entry(student).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }
public static void AccountOrderEFTest() { using (var context = new AccountEntities()) { //删除之前的测试数据 context.Database.ExecuteSqlCommand("delete from chapter3.[order]"); context.Database.ExecuteSqlCommand("delete from chapter3.account"); //添加新的测试数据 var account1 = new Account { City = "Raytown", State = "MO" }; account1.Orders.Add(new Order { Amount = 223.09M, ShipCity = "Raytown", ShipState = "MO" }); account1.Orders.Add(new Order { Amount = 189.32M, ShipCity = "Olathe", ShipState = "KS" }); var account2 = new Account { City = "Kansas City", State = "MO" }; account2.Orders.Add(new Order { Amount = 99.29M, ShipCity = "Kansas City", ShipState = "MO" }); var account3 = new Account { City = "North Kansas City", State = "MO" }; account3.Orders.Add(new Order { Amount = 102.29M, ShipCity = "Overland Park", ShipState = "KS" }); context.Accounts.Add(account1); context.Accounts.Add(account2); context.Accounts.Add(account3); context.SaveChanges(); } using (var context = new AccountEntities()) { var orders = from o in context.Orders join a in context.Accounts on new { Id = o.AccountId, City = o.ShipCity, State = o.ShipState } equals new { Id = a.AccountId, City = a.City, State = a.State } select o; Console.WriteLine("Orders shipped to the account's city, state..."); foreach (var order in orders) { Console.WriteLine("\tOrder {0} for {1}", order.AccountId.ToString(), order.Amount.ToString()); } } }
/// <summary>保存验证码</summary> public static Guid SaveVerifyCode(string verifyCodeText) { if(string.IsNullOrWhiteSpace(verifyCodeText)) throw new Exception("输入的验证码不能为空!"); using (AccountEntities dbContext = new AccountEntities()){ //CreateTime不可空,虽然有默认值,但Linq要显式传入时间,否则会变成NULL而出错 VerifyCode verifyCode = new VerifyCode { VerifyText = verifyCodeText, Guid = Guid.NewGuid(), CreateTime = DateTime.Now }; dbContext.VerifyCode.Add(verifyCode); dbContext.SaveChanges(); //不加Try/Catch的话不会弹出错误 return verifyCode.Guid; } }
/// <summary>检查验证码</summary> public static bool CheckVerifyCode(string verifyCodeText,Guid guid) { using (AccountEntities accountEntities = new AccountEntities()){ VerifyCode verifyCode = accountEntities.VerifyCode.FirstOrDefault(x => x.Guid == guid && x.VerifyText == verifyCodeText); if(verifyCode != null){ //验证成功后删除本条验证码 accountEntities.VerifyCode.Remove(verifyCode); accountEntities.SaveChanges(); //清除验证码大于2分钟还没请求的 DateTime expiredTime = DateTime.Now.AddMinutes(-2); accountEntities.VerifyCode.Where(x => x.CreateTime < expiredTime).Delete(); //Extend扩展方法 return true; } return false; } }