c# - 如果第一次使用代码优先方法会出现迁移错误

标签 c# asp.net-mvc entity-framework asp.net-mvc-4 ef-code-first

我的 MVC 5 项目使用代码优先方法创建数据库。但是在启用迁移后,我收到错误

Migrations is enabled for context 'ApplicationDbContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.

但是,如果我删除迁移文件夹,则错误已解决,但我无法使用迁移。请帮忙。我附上错误的屏幕截图。

enter image description here

我为 add-migration 运行命令,然后为 update-database 运行命令。那确实帮助了我。但是,如果我更改连接字符串并再次运行该项目,那么它不会创建数据库,直到迁移文件夹未被删除。

最佳答案

在您可以使用您的数据库之前,您需要创建一个迁移,它使用 Add-Migration 命令描述您的数据库结构,然后使用 更新您的数据库以匹配您当前的模型>更新数据库命令。

您可能会找到 this tutorial有用。

关于c# - 如果第一次使用代码优先方法会出现迁移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33764867/

相关文章:

c# - 多层架构和身份。为什么要使用UserManager和RoleManager?

c# - 如何根据优先级组合列表

c# - 基于 ASP.NET Core 中的现有数据库创建 Entity Framework 模型

c# - MVC 5 EF 6 TPT 和 ViewModel

c# - 有条件地包含和 ThenInclude

mysql - 如何在 visual studio 2015 中使用 ADO.net Identity 数据模型使用 Entity Framework 6(web api)进行 mariadb 设置(版本 10.2.17)?

c# - 在两个 asp.net web 应用程序之间共享相同的 SessionId

c# - 如何调用 IEnumerable 函数

c# - 用ftp上传文件?

asp.net-mvc - Sitecore + Glass : Render link, 但忽略权限