C# 支持';EFDbContext';自创建数据库以来,上下文已更改
我的sql数据库表和实体框架数据库上下文和模型类正确,但我得到的上下文已更改错误:C# 支持';EFDbContext';自创建数据库以来,上下文已更改,c#,sql,linq,entity-framework,asp.net-mvc-5,C#,Sql,Linq,Entity Framework,Asp.net Mvc 5,我的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/?Lin
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)
);
class EFDbContext : DbContext
{
public DbSet<AppInformation> AppInformation { get; set; }
//public DbSet<Revision> Revisions { get; set; }
public DbSet<James> Jamies{ get; set; }
}
public class James
{
[Key]
public int JamesID { get; set; }
[Required]
[MaxLength(255)]
public string Name { get; set; }
}
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);
}
我的EFDbContext类如下所示:
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)
);
class EFDbContext : DbContext
{
public DbSet<AppInformation> AppInformation { get; set; }
//public DbSet<Revision> Revisions { get; set; }
public DbSet<James> Jamies{ get; set; }
}
public class James
{
[Key]
public int JamesID { get; set; }
[Required]
[MaxLength(255)]
public string Name { get; set; }
}
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);
}
JamesRepository如下所示:
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)
);
class EFDbContext : DbContext
{
public DbSet<AppInformation> AppInformation { get; set; }
//public DbSet<Revision> Revisions { get; set; }
public DbSet<James> Jamies{ get; set; }
}
public class James
{
[Key]
public int JamesID { get; set; }
[Required]
[MaxLength(255)]
public string Name { get; set; }
}
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);
}
有什么想法吗?添加
Database.SetInitializer<EFDbContext>(null);
Database.SetInitializer(null);
转到Global.asax文件以禁用ef迁移。
您似乎出于某种原因启用了迁移。快速修复,删除数据库并运行程序。。否则请使用DatabaseMigration可能重复的