public IList<ProductInfo> GetProductList(int pageIndex, int pageSize, out int Total) { using (MyContext db = new MyContext()) { Total = (from c in db.Product orderby c.ID select c).Count(); var items = (from c in db.Product orderby c.ID select c).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); IList<ProductInfo> ProductInfos = new List<ProductInfo>(); foreach (var item in items) { ProductInfo info = new ProductInfo(); info.ID = item.ID; info.ProductTypeID = item.ProductTypeID; info.ProductTypeName = item.ProductType.ProductTypeName;//导航属性的特点 info.Image = item.Image; info.ProductName = item.ProductName; info.MarketPrice = item.MarketPrice; info.NewPrice = item.NewPrice; info.GetDate = item.GetDate.ToShortDateString(); info.Enable = item.Enable; ProductInfos.Add(info); } return ProductInfos; } }
public ActionResult Index() { var ef = new MyContext(); //ef.Database.Create();//方法二:直接执行生成数据库 return View(); }
public IList<ProductTypeInfo> GetProductTypesBySerach(int pageIndex, int pageSize, out int Total, string name) { using (MyContext db = new MyContext()) { Total = (from c in db.ProductType where (!string.IsNullOrEmpty(name) ? c.ProductTypeName.Contains(name) : true) orderby c.ID select c).Count(); var items = (from c in db.ProductType where (!string.IsNullOrEmpty(name) ? c.ProductTypeName.Contains(name) : true) orderby c.ID select c).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); IList<ProductTypeInfo> ProductTypeInfos = new List<ProductTypeInfo>(); foreach (var item in items) { ProductTypeInfo info = new ProductTypeInfo(); info.ID = item.ID; info.ProductTypeName = item.ProductTypeName; info.Description = item.Description; ProductTypeInfos.Add(info); } return ProductTypeInfos; } }
public static Student GetStudentByID(int sid) { using (MyContext context = new MyContext()) { var query = from p in context.Students where p.StudentID == sid select p; return query.FirstOrDefault(); } }
public static List<Student> GetAllStudents() { using (MyContext context = new MyContext()) { var query = from p in context.Students orderby p.StudentID descending select p; return query.ToList(); } }
public static void DeleteStudentByID(int sid) { using (MyContext context = new MyContext()) { var query = from p in context.Students where p.StudentID == sid select p; var std = query.FirstOrDefault(); context.Students.Remove(std); context.SaveChanges(); } }
public SimpleMembershipInitializer() { Database.SetInitializer(new DBInitializer()); try { using (var context = new DAL.MyContext()) { if (!context.Database.Exists()) { ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("MyContext", "Users", "UserID", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized", ex); } }
public static int SaveStudent(Student stdObj) { using (MyContext context = new MyContext()) { if (stdObj.StudentID > 0) { var std = context.Students.Where(p => p.StudentID == stdObj.StudentID).FirstOrDefault(); std.Name = stdObj.Name; std.Address = stdObj.Address; std.Age = stdObj.Age; } else { context.Students.Add(stdObj); } context.SaveChanges(); } return stdObj.StudentID; }
public IList<ProductTypeInfo> GetProductTypesList() { using (MyContext db = new MyContext()) { var items = db.ProductType.ToList(); IList<ProductTypeInfo> ProductTypeInfos = new List<ProductTypeInfo>(); foreach (var item in items) { ProductTypeInfo info = new ProductTypeInfo(); info.ID = item.ID; info.ProductTypeName = item.ProductTypeName; info.Description = item.Description; ProductTypeInfos.Add(info); } return ProductTypeInfos; } }
public IList<ProductInfo> GetProductsBySerach(int pageIndex, int pageSize, out int Total, int typeId, int id, string name, int NewPrice, bool? b) { using (MyContext db = new MyContext()) { Total = (from c in db.Product where ((id != 0) ? c.ID == id : true) && ((typeId != 0) ? c.ProductTypeID == typeId : true) && (!string.IsNullOrEmpty(name) ? c.ProductName.Contains(name) : true) && ((NewPrice != 0) ? c.NewPrice == NewPrice : true) && ((b == null) ? true : c.Enable == b)//传值为NULL就是全部 orderby c.ProductTypeID select c).Count(); var items = (from c in db.Product where ((id != 0) ? c.ID == id : true) && ((typeId != 0) ? c.ProductTypeID == typeId : true) && (!string.IsNullOrEmpty(name) ? c.ProductName.Contains(name) : true) && ((NewPrice != 0) ? c.NewPrice == NewPrice : true) && ((b == null) ? true : c.Enable == b)//传值为NULL就是全部 orderby c.ProductTypeID//必须在分页前排序 select c).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); List<ProductInfo> ProductInfos = new List<ProductInfo>(); foreach (var item in items) { ProductInfo info = new ProductInfo(); info.ID = item.ID; info.ProductTypeID = item.ProductTypeID; info.ProductTypeName = item.ProductType.ProductTypeName;//导航属性的特点 info.Image = item.Image; info.ProductName = item.ProductName; info.MarketPrice = item.MarketPrice; info.NewPrice = item.NewPrice; info.GetDate = item.GetDate.ToShortDateString(); info.Enable = item.Enable; ProductInfos.Add(info); } return ProductInfos; } }
public IList<ProductTypeInfo> GetProductTypesList(int pageIndex, int pageSize, out int Total) { using (MyContext db = new MyContext()) { Total = (from c in db.ProductType orderby c.ID select c).Count(); var items = (from c in db.ProductType orderby c.ID select c).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); IList<ProductTypeInfo> ProductTypeInfos = new List<ProductTypeInfo>(); foreach (var item in items) { ProductTypeInfo info = new ProductTypeInfo(); info.ID = item.ID; info.ProductTypeName = item.ProductTypeName; info.Description = item.Description; ProductTypeInfos.Add(info); } return ProductTypeInfos; } }
public bool GreateProduct(Product info) { using (MyContext db = new MyContext()) { bool a = false; try { db.Product.Add(info); int b = db.SaveChanges(); if (b == 1) { a = true; } return a; } catch (Exception ex) { return a; } } }
public bool UpdateProduct(Product info) { using (MyContext db = new MyContext()) { bool a = false; try { db.Entry(info).State = EntityState.Modified; // int b = db.SaveChanges(); if (b == 1) { a = true; } return a; } catch (Exception ex) { return a; } } }
public int RemeProduct(string[] Ids) { using (MyContext db = new MyContext()) { int num = 0; try { foreach (string productid in Ids) { int id = Convert.ToInt32(productid); var item = db.Product.Single(g => g.ID == id); db.Product.Remove(item); num = db.SaveChanges(); } return num; } catch (Exception ex) { return num; } } }