Ejemplo n.º 1
0
        public ActionResult Index()
        {
            var ef = new MyContext();
            //ef.Database.Create();//方法二:直接执行生成数据库

            return View();
        }
Ejemplo n.º 2
0
        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;
            }
        }
Ejemplo n.º 3
0
        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;
            }
        }
Ejemplo n.º 4
0
        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();

            }
        }
Ejemplo n.º 5
0
        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();

            }
        }
Ejemplo n.º 6
0
        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();

            }
        }
Ejemplo n.º 7
0
        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;
        }
Ejemplo n.º 8
0
        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;

            }
        }
Ejemplo n.º 9
0
        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;
            }
        }
Ejemplo n.º 10
0
        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;
            }
        }
Ejemplo n.º 11
0
        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;
                }
            }
        }
Ejemplo n.º 12
0
        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;
                }
            }
        }
Ejemplo n.º 13
0
        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;
                }

            }
        }