C# – The model backing the ‘EFDbContext’ context has changed since the database was created

asp.net-mvc-5centity-frameworklinqsql

My sql database table and Entity framework database context and Model class are correct yet i get a context has changed error:

Additional information: The model backing the 'EFDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

My Table looks like this:

CREATE TABLE [dbo].[Jamies] (
    [JamesID] INT            IDENTITY (1, 1) NOT NULL,
    [Name]   NVARCHAR (255) NOT NULL,
    CONSTRAINT [PK_dbo.Jamies] PRIMARY KEY CLUSTERED ([JamesID] ASC)
);

My EFDbContext class looks like this:

class EFDbContext : DbContext
{
    public DbSet<AppInformation> AppInformation { get; set; }
    //public DbSet<Revision> Revisions { get; set; }

    public DbSet<James> Jamies{ get; set; }
}

My James class looks like this:

public class James
{
    [Key]
    public int JamesID { get; set; }

    [Required]
    [MaxLength(255)]
    public string Name { get; set; }
}

The JamesRepository looks like this:

public class EFJamesRepository : IJamesRepository
{
    private EFDbContext context = new EFDbContext();

    public IQueryable<James> Jamies
    {
        get { return context.Jamies; }
    }
...

My controller method going wrong looks like this:

public class JamesController : Controller
{
    private IJamesRepository repository;
    public int PageSize = 2;

    public JamesController(IJamesRepository repo)
    {
        repository = repo;
    }

    public ViewResult List(int page = 1)
    {
        JamiesListViewModel model = new JamiesListViewModel
        {
            Jamies = repository.Jamies
                .OrderBy(s => s.Name)
                .Skip((page - 1) * PageSize)
                .Take(PageSize),
            PagingInfo = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = repository.Jamies.Count()
            }
        };
        return View(model);
    }

Any ideas?

Best Answer

Add

Database.SetInitializer<EFDbContext>(null);

To your Global.asax file to disable ef migrations. It seems you have migrations enabled for some reason.