迁移中如何使用DbContext?

2023-12-29

我该如何使用DbContext它适用于当前数据库(现在在迁移中使用)。

Example:

namespace Data.SqlServer.Migrations
{
    [DbContext(typeof(MyDbContext))]     // I want use this context
    [Migration("CustomMigration_DataSeed")]
    public partial class DataSeedMigration : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // add some entities
            _context.User.Add(new User());
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
        }
    }
}

感谢帮助!


为您的迁移配置创建一个类:

internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        //On true you might be losing data be aware. 
        AutomaticMigrationDataLossAllowed = false;
        ContextKey = "Path To Your DbContext";
    }

    protected override void Seed(MyDbContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

然后将其引用到您的 DbContext 类:

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("name=MyConnection")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext,Configuration>("MyConnection")); 
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //here you can MAP Your Models/Entities
    }
}

请记住,如果您不想迁移多个 POCO,则不要将它们添加到 OnModelCreating 方法中并对其进行注释。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

迁移中如何使用DbContext? 的相关文章

随机推荐

  • 使用 InputField 时隐藏虚拟键盘

    我知道如何使用隐藏或显示虚拟键盘InputMethodManager 但我想输入文字EditText使用物理键盘 但我不想在 Unity 3D Android 中显示虚拟键盘 我怎样才能在 Unity 3D 中做到这一点 不存在这样的事情E
  • 如何在 Cloud Dataflow 中使用 TextIO.Read 将多个文件与名称进行匹配

    我有一个 gcs 文件夹 如下所示 gs
  • 在 Swift 中,仅在对象的生命周期内调用一次方法

    假设我们覆盖scrollViewDidScroll方法和第一次滚动only 我们想doSomething 实现这一点的一个简单方法是拥有一个实例级布尔变量并切换 检查它 func scrollViewDidScroll scrollView
  • Dojo:在 required=false 时为 FilteringSelect 选择空值

    请看一下这段代码 dojo require dijit form FilteringSelect dojo require dojo store JsonRest dojo declare JsonFilteringSelect dijit
  • 结账时修改 Shopify 运费

    Is there a possibility that we could add a custom shipping price at checkout through an API call or script 我想根据一些规则修改价格
  • Array.isDefinedAt 用于 scala 中的 n 维数组

    有没有一种优雅的表达方式 val a Array fill 2 10 1 def do to elt i Int j Int if a isDefinedAt i a i isDefinedAt j f a i j 在斯卡拉 我建议您不要将
  • OpenXML - SpreadsheetReader 在当前上下文中不存在

    所以我试图复制这个答案使用 Openxml 2 0 在 Excel 中获取单元格背景颜色 https stackoverflow com questions 10756206 getting cell backgroundcolor in
  • 许可检查器的“已激活”状态的寿命。对于用户来说,多频繁才算过多?

    我的应用程序是一种袖珍指南 包含全年的一些数据 我假设用户将至少使用该程序一次并通过互联网访问来激活许可证 或者至少这是我对 Android 许可系统如何工作的理解 我正在使用服务器管理策略 它会定期检查许可证吗 有什么办法可以控制这个吗
  • 将 docker 卷挂载到 Windows 主机

    我正在我的 Windows 10 机器上运行 docker for windows 使用 hyper v 如果我运行以下命令 docker volume create test docker volume inspect test I ge
  • MongoDb 中的 $push 不起作用?

    我的架构如下所示 var exampleSchema newSchema profile experience exp String 这是更新个人资料收集中的经验的代码 exampleSchema statics experience fu
  • 如何在anjuta项目中包含资源文件

    我正在尝试更新 vala 中的图形项目 将大量代码行移动到 ui 文件中 我想使用模板 可用于 glib 2 38 和 GTK 3 8 类似的东西 我的项目是用 Anjuta 和 autoconf 管理的 In the src目录有 app
  • 如何在 PHP 中进行 URL 重写?

    我正在尝试在我的 PHP 应用程序中实现 URL 重写 有人可以分享一下在 PHP 和 MySQL 中实现 URL 重写的分步过程吗 在我的应用程序中 我想实现以下 URL 重写 我想重定向 1 http example com video
  • Firefox ... SVG ... 下划线...天哪?

    到目前为止 我尝试的任何方法都无法让 Firefox 在某些文本下划线 根据谷歌的结果 这个问题已知并记录了五年多 这不可能是真的 发生了什么事 有人知道任何持续有效的解决方法 甚至可能知道他们为什么还没有修复它的原因 我对此很好奇 Fir
  • OCaml:模式匹配与 If/else 语句

    因此 我对 OCaml 完全陌生 并且在实现第一个功能方面进展相当缓慢 我无法理解的一件事是何时使用模式匹配功能 例如 let foo gt true gt false 与使用 if else 结构类似 let foo a if a the
  • 如何在部署时在Heroku中自定义pip的requirements.txt?

    我正在将 Python Django 应用程序部署到 Heroku 并且仅当部署到 Heroku 等某些环境时 我才想自定义requirements txt 文件 通过添加 git 支持的依赖项和密码 我很想将其设置在环境变量或其他变量中
  • 在模板类实现中使用struct template

    我正在尝试学习 C 中的模板用法 我创建了一个结构节点 在队列类实现中使用它 但出现编译器错误 错误 成员函数 bool MyQueue add T data 中 qnode 之前的预期类型说明符 include
  • 获取 R 中某个因子中出现次数最多的元素

    我在 R 变量中有一组字符串 当我检查类时 它说它是一个因素 例如 mySet lt c abc abc def abc def efg abc 我想获取该集合中出现次数最多的字符串 即本例中的 abc 我理解一种方法是使用hist 但我面
  • 安装 Visual Studio 扩展

    我刚刚开始使用 VS2015 但在做一些本应简单的事情时遇到了麻烦 安装 Side Waffle 扩展 我从他们的网站下载了 vsix 文件 templatepack vsix 当我双击它时 我收到一条消息 扩展清单无效 因此 我进入 Vi
  • 关于c中的表达式“&anArray”

    首先 我读到 array array array 0 只要 array 确实是一个数组 一切都会相同 所以我尝试 int main char ar a b c 0 printf argument ar s n ar printf argum
  • 迁移中如何使用DbContext?

    我该如何使用DbContext它适用于当前数据库 现在在迁移中使用 Example namespace Data SqlServer Migrations DbContext typeof MyDbContext I want use th