我对实体框架完全陌生,并试图在ASP.NET站点的Rick教程的基础上自己构建一些东西。我创建了一个名为HostelName的模型和一个名为DBContext的DBContext我的解决方案名是- LaundryManagementSystem.
这是我的模型代码:
public class HostelName
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Code { get; set; }
}
public class HostelNameDBContext : DbContext
{
public DbSet<HostelName> HostelNames { get; set; }
}可能是因为在创建模型类之后添加了[Required]属性,所以我面临以下异常:
InvalidOperationException: The model backing the 'HostelNameDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)
我尝试了以下几点:
在我的Visual 2012中,转到TOOLS->NuGet > Package控制台,然后在控制台中键入Enable-Migrations -ContextTypeName LaundryManagementSystem.Models.HostelName,然后构建并再次运行解决方案,但我仍然得到上述异常。
请注意,这是实体框架,代码第一方法。
请有人指出我的问题在哪里,我可能错过了什么,以及如何解决这个问题?
发布于 2015-04-27 08:25:25
启用迁移之后,您需要创建迁移,然后更新数据库。
要在nuget控制台中创建迁移,运行Add-Migration AddedRequiredToProperties。这应该会创建迁移文件。
然后运行Update-Database -Verbose将迁移应用于数据库。这将在数据库上运行迁移脚本,更改表结构。
https://stackoverflow.com/questions/29890435
复制相似问题