自创建数据库以来,支持'EFDbContext'上下文的模型已更改

时间:2014-01-03 11:57:20

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

我的sql数据库表和Entity框架数据库上下文和Model类是正确的但我得到一个上下文已经改变错误:

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

我的表格如下:

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

我的EFDbContext类如下所示:

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

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

我的James课程看起来像这样:

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

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

JamesRepository看起来像这样:

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

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

我的控制器方法出错了如下:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:17)

添加

Database.SetInitializer<EFDbContext>(null);

到您的Global.asax文件以禁用ef迁移。 似乎您出于某种原因启用了迁移。