// private static UserInfo _users = new UserInfo(); protected void Page_Load(object sender, EventArgs e) { FinalEntities con = new FinalEntities(); UserInfo user = new UserInfo(); user.Email = Request.Form["email"]; user.Gender = Request.Form["gender"]; user.Password = Request.Form["password"]; user.Confirmpassword = Request.Form["conpassword"]; user.Username = Request.Form["username"]; con.UserInfoes.Add(user); con.SaveChanges(); }
public DbContext GetDbContext() { //从当前线程中 获取 EF上下文对象 DbContext dbContext = CallContext.GetData(typeof(DBContextFactory).Name) as DbContext; if (dbContext == null) { dbContext = new FinalEntities(); dbContext.Configuration.ValidateOnSaveEnabled = false; //将新创建的 ef上下文对象 存入线程 CallContext.SetData(typeof(DBContextFactory).Name, dbContext); } return(dbContext); }
//insert a product method public string InSetProduct(Product product) { try { FinalEntities db = new FinalEntities(); db.Products.Add(product); db.SaveChanges();//commit //return the got product.Name for the lable which is resultLabel to display return product.Name + " was successfully inserted."; } catch (Exception e) { return e.Message + "no idea"; } }
//insert product type method public string InSetProductType(ProductType productType) { try { FinalEntities db = new FinalEntities(); db.ProductTypes.Add(productType); db.SaveChanges();//commit return productType.TypeDescreption + " was successfully inserted."; } catch (Exception e) { return e.Message; } }
//get product from db public Product GetProduct(int id) { try { //get the database FinalEntities db = new FinalEntities(); //get the table from db Product pt = db.Products.Find(id); return pt; } catch (Exception e) { return null; } }
protected void Page_Load(object sender, EventArgs e) { string p = Request.Form["username"]; string y = Request.Form["password"]; FinalEntities con = new FinalEntities(); UserInfo user = new UserInfo(); var t = con.UserInfoes.FirstOrDefault(x => x.Username == p); if (t != null) { if (Request.Form["username"] != null) { Response.Redirect("~/View/User.aspx"); } Response.Redirect("~/View/Login.aspx"); //Response.Write("Successful"); } else { Response.Redirect("~/View/Login.aspx"); } }
//delete a product by the id public string DeleteProduct(int id) { try { //get the database FinalEntities db = new FinalEntities(); //get the table from db Product pt = db.Products.Find(id); //attach() method Attaches a disconnected or "detached" entity to a new DataContext when original values are required for optimistic concurrency checks. db.Products.Attach(pt); //remove the product type by the id db.Products.Remove(pt); //save it db.SaveChanges(); //return the got name for the lable which is resultLabel return pt.Name + "was successfully deleted."; } catch (Exception e) { return e.Message; } }
//update the product by the id public string UpdateProduct(int id, Product product) { try { //get the database FinalEntities db = new FinalEntities(); //get the table from db Product pt = db.Products.Find(id); //use the pt id to get all the variables from db pt.Name = product.Name; pt.Image = product.Image; pt.Price = product.Price; pt.Type = product.Type; pt.Quantity = product.Quantity; pt.Description = product.Description; //commit it save it db.SaveChanges(); //return the got Name for the lable which is resultLabel to display return product.Name + "was successfully updated."; } catch (Exception e) { return e.Message; } }
//update product type by the id public string UpdateProductType(int id, ProductType productType) { try { //get the database FinalEntities db = new FinalEntities(); //get the table from db ProductType pt = db.ProductTypes.Find(id); //use the pt id to get the TypeDescreption from db pt.TypeDescreption = productType.TypeDescreption; //commit it save it db.SaveChanges(); //return the got TypeDescreption for the lable which is resultLabel return productType.TypeDescreption + "was successfully updated."; } catch (Exception e) { return e.Message; } }