为什么我的实体框架网站无法正常工作?

时间:2018-09-05 14:17:05

标签: asp.net-mvc entity-framework-6

我正在学习ASP.NET MVC,并正在尝试开发应用程序。

我在使用实体框架代码优先时当前遇到错误。但是,当我执行一个应用程序时,我的浏览器显示的错误消息如Pic1所示,而代码显示的错误则显示在Pic2中

Pic1

enter image description here Pic2

enter image description here 这是3个可以让您更加清晰的课程

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
    OdeToFood _db = new OdeToFood();

    public ActionResult Index()
    {
        var model = _db.Restaurants.ToList();

        return View(model);
    }

    public ActionResult About()
    {
        //ViewBag.MessageA = "Your application description page.";

        var model = new AboutModel();
        model.Name = "Jimmit Mukesh Raval";
        model.Location = "Mulund";

        ViewBag.Message = "Jimmit Mukesh Raval";

        return View(model);
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}
}

Restaurant.cs

namespace WebApplication1.Models
{
public class Restaurant
{
    public int Id { get; set; }
    public string Name{ get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public ICollection<RestaurantReview> Reviews{ get; set; }
}
}

RestaurantReview.cs

namespace WebApplication1.Models
{
public class RestaurantReview
{
    public int Id { get; set; }
    public int Rating { get; set; }
    public string Body { get; set; }
    public string RestaurantId { get; set; }
}
}

OdeToFood.cs

namespace WebApplication1.Models
{
public class OdeToFood:DbContext
{
    public DbSet<Restaurant> Restaurants { get; set; }
    public DbSet<RestaurantReview> Reviews { get; set; }
}
}

任何人都可以帮助我,我哪里不对?

1 个答案:

答案 0 :(得分:0)

尝试添加:

 public class OdeToFood : DbContext
    {
        public OdeToFood()
         : base("OdeToFood")
        {
        }
        public DbSet<Restaurant> Restaurants { get; set; }
        public DbSet<RestaurantReview> Reviews { get; set; }
    }