Why is my Entity Framework site not working?

asp.net-mvc entity-framework-6

Question

I am learning ASP.NET MVC and practicing developing an application.

I am currently facing an error where I am using Entity Framework code-first. But when I execute an application my browser is showing the error message as in Pic1 and the code show error in Pic2

Pic1

enter image description here Pic2

enter image description here Here are the 3 classes that may give you more clarity

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; }
}
}

Can anyone please help me, where am I getting incorrect?

1
0
9/5/2018 2:26:35 PM

Accepted Answer

Try add:

 public class OdeToFood : DbContext
    {
        public OdeToFood()
         : base("OdeToFood")
        {
        }
        public DbSet<Restaurant> Restaurants { get; set; }
        public DbSet<RestaurantReview> Reviews { get; set; }
    }
0
9/5/2018 8:06:09 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow