gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:52:55 28 4
gpt4 key购买 nike

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

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

我的 Controller 方法出错是这样的:

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

有什么想法吗?

最佳答案

添加

Database.SetInitializer<EFDbContext>(null);

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

关于c# - 自数据库创建以来支持 'EFDbContext' 上下文的模型已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20903123/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com