EF的入门使用 (电影管理)

Posted 惜取少年时,只为不流泪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF的入门使用 (电影管理)相关的知识,希望对你有一定的参考价值。

 

控制器代码:

技术分享
    public class HomeController : Controller
    {
        private NewDBContext ndc = new NewDBContext();

        public ActionResult index(string search)
        {
            List<Movie> b = ndc.movies.ToList();
            if (string.IsNullOrEmpty(search))
                b.Where(x => x.Title == search).ToList();

            ViewData["Title"] = b.Select(a => new SelectListItem { Value = a.Title, Text = a.Title }).ToList();
            return View(b);
        }
        [HttpPost]
        public string Index(string search)
        {
            return "this post " + search;
        }

        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Movie b)
        {
            if (ModelState.IsValid)
            {
                ndc.movies.Add(b);
                ndc.SaveChanges();
            }
            return RedirectToAction("index");
        }

        public ActionResult Edit(int id)
        {
            Movie m = ndc.movies.Find(id);
            if (m == null)
                return HttpNotFound();
            return View(m);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Movie m)
        {
            if (ModelState.IsValid)
            {
                ndc.Entry(m).State = EntityState.Modified;
                ndc.SaveChanges();
                return RedirectToAction("index");
            }
            return View(m);
        }


        public ActionResult Details(int id)
        {
            Movie m = ndc.movies.Find(id);
            if (m == null) return HttpNotFound();
            return View(m);
        }


        public ActionResult Delete(int id)
        {
            Movie m = ndc.movies.Find(id);
            if (m == null) return HttpNotFound();
            ndc.movies.Remove(m);
            ndc.SaveChanges();
            return RedirectToAction("index");
        }
            

    }
View Code

 

模型代码

技术分享
    public class Movie
    {
        public int Id { get; set; }
        [Required]
        [Display(Name = "标题")]
        [StringLength(30,MinimumLength =1)]
        public string Title { get; set; }

        [Required]
        [Display(Name = "时间")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
        public DateTime ReleaseDate { get; set; }

        [Required]
        [Display(Name = "作者")]
        public string Genre { get; set; }
        [Display(Name = "价格")]   
        [Range(1,100)]
        [DataType(DataType.Currency)]     
        public decimal Price { get; set; }
    }
    public class NewDBContext : DbContext
    {
        public DbSet<Movie> movies { get; set; }
    }
View Code

---------------------------------------------

以下为数据查找功能

以上是关于EF的入门使用 (电影管理)的主要内容,如果未能解决你的问题,请参考以下文章

Shell编程入门

ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统

如何使用 EF 5 存储枚举列表

EF添加关联的提示问题:映射从第 260 行开始的片段时有问题:

EF Core 入门

如何使用引导程序和 for 循环在 django 中创建电影片段?