c# - 启动时默认运行
问题描述
我正在处理一个测试项目,我正试图围绕迁移展开我的头脑。
我创建了一个名为AngularASPNETCore2WebApiAuth
:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("AngularASPNETCore2WebApiAuth")));
当我开始我的测试项目并出现错误时:
System.Data.SqlClient.SqlException:'无效的对象名称'AspNetUsers'。
我当时的印象是我提供了 MigrationsAssembly,因此可以在项目启动时运行迁移。
我知道我可以使用 Update-Database 命令通过命令行更新我的数据库。
但是,如果默认情况下应用程序不运行迁移,为什么我必须在启动时提供迁移程序集。如何在启动时默认运行迁移?
解决方案
设置 MigrationsAssembly 只是告诉上下文在哪里可以找到迁移程序集,您仍然需要显式运行它(很多时候您不想在每次启动应用程序时都运行迁移)。你需要打电话
myDbContext.Database.Migrate();
执行迁移。
https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#apply-migrations-at-runtime
推荐阅读
- elasticsearch - 在 Elasticsearch 中使用下一个单词建议自动完成
- uwp - IHttpFilter 实现导致 HttpClient::GetStringAsync 调用的访问冲突
- perl - 打印语句在 perl 的外部块中打印两次
- java - Javafx gridpane 行索引没有添加到正确的位置
- java - JPA - 当参数是属性的成员时选择,属性是一组字符串
- objective-c - Objective-C中的条形图?
- java - 通过 Spring 将数据从 Java 传递到 html
- python - REST 框架编写自定义字段验证
- python - 通过创建发送和 Python 使用对 Campaign Monitor 的 API 调用获取订阅者
- javascript - 从控制器动态添加类