首页 > 解决方案 > 从 SQL 数据库创建

问题描述

我一步一步地遵循这个: https ://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/getting-started-with-mvc/getting-started-with-mvc-第 6 部分

我的问题是添加创建页面时,这是从哪里来的?db.AddToMovies(newMovie) - 它给出了一个构建错误(下面的控制器代码)?

这是从 Visual Studio 2019 列出、创建和编辑 SQL 表的最佳方式吗?

谢谢, EB

我已经在控制器页面上添加了它。那是SP还是数据库中的东西?db.AddToEmployees(newEmployee); 正在给出问题。这不是我试图自定义的代码:

                    IntranetEntities db = new IntranetEntities();
                    public ActionResult Employees()
                    {
                        var employees = from m in db.Employees
                                        select m;

                        return View(employees.ToList());

                    }


                    // GET: Employees/Details/5
                    public ActionResult Details(int id)
                    {
                        return View();
                    }

                    // GET: Employees/Create
                    public ActionResult Create()
                    {
                        return View();
                    }

                    // POST: Employees/Create
                    [HttpPost]

                    public ActionResult Create(Employee newEmployee)
                    {

                        if (ModelState.IsValid)
                        {
                            db.Employees(newEmployee);
                            db.SaveChanges();

                            return RedirectToAction("Employees");
                        }
                        else
                        {
                            return View(newEmployee);
                        }
                    }

标签: asp.net-mvc

解决方案


你可以去上一页,它声明MoviesEntities db = new MoviesEntities();

public class MoviesController : Controller
    {
        MoviesEntities db = new MoviesEntities();

        public ActionResult Index()
        {
            var movies = from m in db.Movies
                         where m.ReleaseDate > new DateTime(1984, 6, 1)
                         select m;

            return View(movies.ToList());

        }
    }

https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/getting-started-with-mvc/getting-started-with-mvc-part5

更新:

AddToMovies可能是数据库中的存储过程,但我在 MSDN 示例中也找不到它。

你可以通过改变来解决

db.AddToMovies(newMovie)

_db.Movies.Add(newMovie)

推荐阅读