0

Trying to populate lists from my database as follows, here is the controller:

namespace TimetableSystem.Controllers
{
public class AvailabilityController : Controller
{
    TimetableSystemEntities systemDB = new TimetableSystemEntities();

    public ActionResult Index()
    {

        var parkList = new List<String>();
        var parkQry = from p in systemDB.Parks
                      orderby p.ParkID
                      select p.ParkName;
        parkList.AddRange(parkQry);
        ViewBag.Park = parkList;

here is the park model:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace TimetableSystem.Models
{
    public class ParkModel
    {
        [Key]
        public int ParkID { get; set; }
        public string ParkName { get; set; }
    }
}

and here is the TimetableSystemEntities model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace TimetableSystem.Models
{
    public class TimetableSystemEntities : DbContext
    {
        public TimetableSystemEntities() : base("DefaultConnection") { }

        public DbSet<Department> Departments { get; set; }
        public DbSet<Module> Modules { get; set; }
        public DbSet<Request> Requests { get; set; }
        public DbSet<Round> Rounds { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<ParkModel> Parks { get; set; }
        public DbSet<BuildingModel> Buildings { get; set; }
        public DbSet<RoomModel> Rooms { get; set; }
    }
}

But when I debug and move to the Availability page, I am given this error:

[System.Data.SqlClient.SqlException] {"Invalid object name 'dbo.ParkModels'."}

Not sure what is happening here as I can't find 'ParkModels' anywhere in the project or the database, the model is ParkModel, the database table is Park.

4
  • Change DbSet<ParkModel> to DbSet<Park> Commented Mar 26, 2014 at 16:02
  • Thank you but that just changes the error to {"Invalid object name 'dbo.Parks'."} Commented Mar 27, 2014 at 15:59
  • This is one of those questions that would benefit from accepting an answer by the community. Commented Sep 30, 2017 at 10:21
  • I think, you will need add migration for your Context and update database. If you are using EF6 for connection database, this link help you Commented Sep 7, 2020 at 5:35

1 Answer 1

1

My understanding of code-first is that by convention the model and table names need to match. In your case they don't so you need to specify the mapping.

[Table("Park")]
public class ParkModel
{
    [Key]
    public int ParkID { get; set; }
    public string ParkName { get; set; }
}

Then your context should be:

DbSet<ParkModel> Parks { get; set; }
Sign up to request clarification or add additional context in comments.

1 Comment

This post is fairly old, so don't know if someone will even see this, but...actually, the code-first convention is not that the model and table names match. It's that the model is the singular of the table name. i.e., model = 'Park' and table = 'Parks'. But, yes, you are correct that when the convention is not followed, you have to specify the table name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.