【发布时间】: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);
}
有什么想法吗?
【问题讨论】:
-
快速修复,删除数据库并运行程序.. 否则使用DatabaseMigration
标签: c# sql linq entity-framework asp.net-mvc-5