【问题标题】:The model backing the 'EFDbContext' context has changed since the database was created自创建数据库以来,支持“EFDbContext”上下文的模型已更改
【发布时间】:2014-01-21 02:12:15
【问题描述】:

我的 sql 数据库表和实体框架数据库上下文和模型类是正确的,但我得到一个上下文已更改错误:

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

有什么想法吗?

【问题讨论】:

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


【解决方案1】:

添加

Database.SetInitializer<EFDbContext>(null);

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

【讨论】:

    猜你喜欢
    • 2014-09-30
    • 1970-01-01
    • 2014-04-19
    • 2011-04-05
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多