The model backing the 'EFDbContext' context has changed since the database was created

asp.net-mvc-5 c# entity-framework linq sql

Question

My Entity Framework database context, SQL database table, and Model class are all fine, but I still receive the 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 is set up as follows:

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

This is how my EFDbContext class looks:

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

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

This is how my James class is set up:

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

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

This is how the JamesRepository looks:

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

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

The problem with my controller technique 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 thoughts?

1
6
1/3/2014 11:57:19 AM

Accepted Answer

Add

Database.SetInitializer<EFDbContext>(null);

Disable ef migrations in your Global.asax file. It appears that for some reason you have migrations enabled.

17
1/3/2014 2:27:11 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