首页 > 其他分享 >EF Core 主从表修改主键类型步骤

EF Core 主从表修改主键类型步骤

时间:2023-05-18 17:44:16浏览次数:40  
标签:Core name Blogs EF Posts BlogId migrationBuilder table 主键

1. 背景

有两张表Blog与Post

classDiagram class Blog { +Guid Id +String Name +DateTime CreateTime +int Order +List<Post> Posts } class Post { +String Id +String Content +Guid BlogId +Blog Blog } Blog --* Post

最开始的设计Blog表的主键是Guid的,但是后期想改成String,又涉及到Post表的修改,步骤记录如下

2. 修改步骤

2.1 修改Post表的外键约束

将之前的外键约束 Cascade 修改为 NoAction

builder.HasOne(b => b.Blog).WithMany(p => p.Posts)
                .HasForeignKey(p => p.BlogId)
                .OnDelete(DeleteBehavior.NoAction);

// 生成的 Migration
public partial class ModifyRelationshipFK : Migration
{
  protected override void Up(MigrationBuilder migrationBuilder)
  {
      migrationBuilder.DropForeignKey(
          name: "FK_Posts_Blogs_BlogId",
          table: "Posts");

      migrationBuilder.AddForeignKey(
          name: "FK_Posts_Blogs_BlogId",
          table: "Posts",
          column: "BlogId",
          principalTable: "Blogs",
          principalColumn: "Id");
  }

  protected override void Down(MigrationBuilder migrationBuilder)
  {
      migrationBuilder.DropForeignKey(
          name: "FK_Posts_Blogs_BlogId",
          table: "Posts");

      migrationBuilder.AddForeignKey(
          name: "FK_Posts_Blogs_BlogId",
          table: "Posts",
          column: "BlogId",
          principalTable: "Blogs",
          principalColumn: "Id",
          onDelete: ReferentialAction.Cascade);
  }
}

2.2 修改主表类型与主键约束

// Blog 修改Id类型为string,Post修改BlogId类型为string
// Post 修改主键约束
builder.HasAlternateKey(b => b.Id).HasName("PK_Blogs_Guid2String");

// Post表修改外键约束
builder.HasOne(b => b.Blog).WithMany(p => p.Posts)
                .HasForeignKey(p => p.BlogId)
                .OnDelete(DeleteBehavior.Cascade);
// 生成的 Migration
public partial class ModifyBlogIdType : Migration
{
  protected override void Up(MigrationBuilder migrationBuilder)
  {
      migrationBuilder.DropForeignKey(
          name: "FK_Posts_Blogs_BlogId",
          table: "Posts");

      migrationBuilder.DropPrimaryKey(
          name: "PK_Blogs",
          table: "Blogs");

      migrationBuilder.AlterColumn<string>(
          name: "BlogId",
          table: "Posts",
          type: "nvarchar(100)",
          maxLength: 100,
          nullable: true,
          oldClrType: typeof(Guid),
          oldType: "uniqueidentifier",
          oldMaxLength: 100);

      migrationBuilder.AlterColumn<string>(
          name: "Id",
          table: "Blogs",
          type: "nvarchar(100)",
          maxLength: 100,
          nullable: false,
          oldClrType: typeof(Guid),
          oldType: "uniqueidentifier",
          oldMaxLength: 100);

      migrationBuilder.AddPrimaryKey(
          name: "PK_Blogs_To_String",
          table: "Blogs",
          column: "Id");

      migrationBuilder.AddForeignKey(
          name: "FK_Posts_Blogs_BlogId",
          table: "Posts",
          column: "BlogId",
          principalTable: "Blogs",
          principalColumn: "Id",
          onDelete: ReferentialAction.Cascade);
  }

  protected override void Down(MigrationBuilder migrationBuilder)
  {
      migrationBuilder.DropForeignKey(
          name: "FK_Posts_Blogs_BlogId",
          table: "Posts");

      migrationBuilder.DropPrimaryKey(
          name: "PK_Blogs_Guid2String",
          table: "Blogs");

      migrationBuilder.AlterColumn<Guid>(
          name: "BlogId",
          table: "Posts",
          type: "uniqueidentifier",
          maxLength: 100,
          nullable: false,
          defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
          oldClrType: typeof(string),
          oldType: "nvarchar(100)",
          oldMaxLength: 100,
          oldNullable: true);

      migrationBuilder.AlterColumn<Guid>(
          name: "Id",
          table: "Blogs",
          type: "uniqueidentifier",
          maxLength: 100,
          nullable: false,
          oldClrType: typeof(string),
          oldType: "nvarchar(100)",
          oldMaxLength: 100);

      migrationBuilder.AddPrimaryKey(
          name: "PK_Blogs",
          table: "Blogs",
          column: "Id");

      migrationBuilder.AddForeignKey(
          name: "FK_Posts_Blogs_BlogId",
          table: "Posts",
          column: "BlogId",
          principalTable: "Blogs",
          principalColumn: "Id");
  }
}

3. 总结

关键点主要在于子表的外键约束与主表的主键约束,首先要删除子表的外键约束并重建,其次要重建主表的主键约束;

标签:Core,name,Blogs,EF,Posts,BlogId,migrationBuilder,table,主键
From: https://www.cnblogs.com/wh-blog/p/17412643.html

相关文章

  • cdr最新2023版本发布更新CorelDRAW 2023下载mac/win
    CorelDRAW2023是一款矢量图形设计软件,由CorelCorporation开发。它提供了一系列强大的工具和功能,可以帮助用户创建专业级的图形设计作品,如标志、海报、名片、包装和插图等。CorelDRAW2023的主要功能包括矢量图形编辑、图形排版、颜色管理和输出预览等。它还提供了智能对象、......
  • vue中pdf预览,报错(Cannot read properties of undefined (reading ‘catch‘))解决
    1、在node_modules中找到vue-pdf,src下的pdfjsWrapper.jsif(pdfRender!==null){if(canceling)return;canceling=true;pdfRender.cancel().catch(function(err){emitEvent('error',err);});return;}修改为if(pdf......
  • 通俗易懂,什么是.NET Core以及.NET Core能做什么
    我们都知道.NETCore是一个可以用来构建现代、可伸缩和高性能的跨平台软件应用程序的通用开发框架。可用于为Windows、Linux和MacOS构建软件应用程序。与其他软件框架不同,.NETCore是最通用的框架,可用于构建各种软件,包括Web应用程序、移动应用程序、桌面应用程序、云服务、微服务、......
  • [图]Firefox 69新功能:可默认同时阻止视频和音频以节省流量
    Mozilla终于开始考虑修复困扰Firefox浏览器数十年的吃带宽问题。在最新的FirefoxNightly69版本中,允许用户同时阻止视频和音频。当前稳定版中,只允许用户阻止音频播放,不过在最新版本中允许用户同时阻止音频和视频,这样在访问Facebook或者Twitter等网站时候就不会出现太耗流量的问题......
  • .NET6 + EF Core + MySQL 创建实体和数据库、EFCore 数据迁移
    前言接上期文章《.NET6项目连接数据库方式方法》,有人问了我几个问题,现在就这几个问题,拓展延申一下创建实体类、数据库。把ORM框架和数据迁移都写进去。安装ORM框架,这里我们采用EFCore安装EFCore我的项目是在Linux上创建的,使用的是vscode开发工具远程开发。为了方便大家阅读和......
  • 正点原子Linux第31章《Uboot顶层Makefile详解》学习
    uboot目录结构1. 文件夹arch:存放关于CPU架构的代码2. 文件夹board:存放关于特定开发板的代码3. 文件夹configs:存放uboot的配置,文件的格式为:xxxxx.deconfig,通过编译(make),生成.config文件。4. 文件u-boot.xxx,大多数为编译相关的或者编译生成的目标文件,例如uboot.imx等等......
  • this.$refs.ref值.toggleRowExpansion is not a function的解决方法
    el-table点击行也能够打开子表,开始搞了个静态(子表)的可以的。但是现次执行这个方法,就报错了。<el-tablev-loading="loading":data="item.steps"style="border-radius:0px!important;"ref="stepTable"......
  • XAF EFCore 示例
    前言在DEV官方建议创建新的XAF项目推荐选择EFCore时,我也第一时间创建了XAF的EFCore项目,这也是我第一次创建这个类型的项目,之前一直使用XPO,避免不了要对比一下。如果熟悉XPO但不了解EFCore的小伙伴来说,会有些迷茫,再加上通过向导创建的项目并不是开箱即用,还需要进行配置,可能会打击......
  • 【愚公系列】2023年05月 .NET CORE工具案例-对象映射Master的使用
    (文章目录)前言对象映射框架Master可以帮助开发人员将对象映射到数据库,以进行数据持久化。它还可以支持ORM(对象关系映射),以及其他数据库技术,比如存储过程。它可以帮助开发人员更快、更有效地完成数据库操作。Master官网:https://github.com/MapsterMapper/Mapster一、对象映射m......
  • Codeforces Round 868 (Div. 2) A-D
    CodeforcesRound868(Div.2) A.A-characteristicintfac[N];map<int,int>mp;voidinit(){fac[1]=0;mp[0]=1;for(inti=2;i<N;i++){fac[i]=fac[i-1]+(i-1);mp[fac[i]]=i;}}voidsolve(){intn=read(),k=rea......