前言

刚开始接触EF Core时本着探索的精神去搞,搞着搞着发现出问题了,后来就一直没解决,觉得很是不爽,借着周末好好看看这块内容。

EntityFramework Core迁移出现对象在数据库中已存在

在EF Core之前对于迁移的命令有很多,当进行迁移出现对象已在数据库中存在时我们通过如何命令即可解决:

  1. Add-Migration Initial -IgnoreChanges

但是在EF Core对于迁移现如今只存在如下两个命令:

  1. dotnet ef migrations add <<migration_name>>
  2. dotnet ef database update

当我们第一次进行初始化迁移时,表结构完全生成通过 dotnet ef migration add initial 来初始化表,当下次再进行迁移时因为这样或者那样无意的操作导致出现如下结果

翻译成英语则是如下的情况:

  1. There is already an object named in the database

如下为第一次初始化的迁移文件,如下:

  1. public partial class initial : Migration
  2. {
  3. protected override void Up(MigrationBuilder migrationBuilder)
  4. {
  5. migrationBuilder.CreateTable(
  6. name: "Blog",
  7. columns: table => new
  8. {
  9. Id = table.Column<int>(nullable: false)
  10. .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
  11. Code = table.Column<string>(nullable: false),
  12. Count = table.Column<int>(nullable: false),
  13. Name = table.Column<string>(nullable: true),
  14. Url = table.Column<string>(nullable: true)
  15. },
  16. constraints: table =>
  17. {
  18. table.PrimaryKey("PK_Blog", x => x.Id);
  19. });
  20.  
  21. migrationBuilder.CreateTable(
  22. name: "Book",
  23. columns: table => new
  24. {
  25. Id = table.Column<int>(nullable: false)
  26. .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
  27. Name = table.Column<string>(nullable: true)
  28. },
  29. constraints: table =>
  30. {
  31. table.PrimaryKey("PK_Book", x => x.Id);
  32. });
  33.  
  34. migrationBuilder.CreateTable(
  35. name: "Category",
  36. columns: table => new
  37. {
  38. Id = table.Column<int>(nullable: false)
  39. .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
  40. Name = table.Column<string>(nullable: true),
  41. ProductId = table.Column<int>(nullable: false)
  42. },
  43. constraints: table =>
  44. {
  45. table.PrimaryKey("PK_Category", x => x.Id);
  46. });
  47.  
  48. migrationBuilder.CreateTable(
  49. name: "Product",
  50. columns: table => new
  51. {
  52. Id = table.Column<int>(nullable: false)
  53. .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
  54. Code = table.Column<string>(nullable: true),
  55. Name = table.Column<string>(nullable: true)
  56. },
  57. constraints: table =>
  58. {
  59. table.PrimaryKey("PK_Product", x => x.Id);
  60. });
  61.  
  62. migrationBuilder.CreateTable(
  63. name: "Post",
  64. columns: table => new
  65. {
  66. Id = table.Column<int>(nullable: false)
  67. .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
  68. BlogId = table.Column<int>(nullable: false),
  69. Content = table.Column<string>(nullable: true),
  70. Title = table.Column<string>(nullable: true)
  71. },
  72. constraints: table =>
  73. {
  74. table.PrimaryKey("PK_Post", x => x.Id);
  75. table.ForeignKey(
  76. name: "FK_Post_Blog_BlogId",
  77. column: x => x.BlogId,
  78. principalTable: "Blog",
  79. principalColumn: "Id",
  80. onDelete: ReferentialAction.Cascade);
  81. });
  82.  
  83. migrationBuilder.CreateTable(
  84. name: "ProductCategory",
  85. columns: table => new
  86. {
  87. Id = table.Column<int>(nullable: false)
  88. .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
  89. CategoryId = table.Column<int>(nullable: false),
  90. ProductId = table.Column<int>(nullable: false)
  91. },
  92. constraints: table =>
  93. {
  94. table.PrimaryKey("PK_ProductCategory", x => x.Id);
  95. table.ForeignKey(
  96. name: "FK_ProductCategory_Category_CategoryId",
  97. column: x => x.CategoryId,
  98. principalTable: "Category",
  99. principalColumn: "Id",
  100. onDelete: ReferentialAction.Cascade);
  101. table.ForeignKey(
  102. name: "FK_ProductCategory_Product_ProductId",
  103. column: x => x.ProductId,
  104. principalTable: "Product",
  105. principalColumn: "Id",
  106. onDelete: ReferentialAction.Cascade);
  107. });
  108.  
  109. migrationBuilder.CreateIndex(
  110. name: "IX_Post_BlogId",
  111. table: "Post",
  112. column: "BlogId");
  113.  
  114. migrationBuilder.CreateIndex(
  115. name: "IX_ProductCategory_CategoryId",
  116. table: "ProductCategory",
  117. column: "CategoryId");
  118.  
  119. migrationBuilder.CreateIndex(
  120. name: "IX_ProductCategory_ProductId",
  121. table: "ProductCategory",
  122. column: "ProductId");
  123. }
  124.  
  125. protected override void Down(MigrationBuilder migrationBuilder)
  126. {
  127. migrationBuilder.DropTable(
  128. name: "Book");
  129.  
  130. migrationBuilder.DropTable(
  131. name: "Post");
  132.  
  133. migrationBuilder.DropTable(
  134. name: "ProductCategory");
  135.  
  136. migrationBuilder.DropTable(
  137. name: "Blog");
  138.  
  139. migrationBuilder.DropTable(
  140. name: "Category");
  141.  
  142. migrationBuilder.DropTable(
  143. name: "Product");
  144. }

此时为了解决上述问题前提是最初的迁移类文件还在,我们需要将Up方法里面的数据全部删除而对于Down方法里面的数据可删除可不删除

  1. public partial class initial : Migration
  2. {
  3. protected override void Up(MigrationBuilder migrationBuilder)
  4. {}
  5.  
  6. protected override void Down(MigrationBuilder migrationBuilder)
  7. {
  8. ........
  9. }
  10. }

通过Up方法来创建表或者修改列,通过Down方法来删除表或者修改列。上面我们创建了BlogType列,此时我们在映射时将其删除同时在Blog类中删除此字段,如下:

  1. public class BlogMap : EntityMappingConfiguration<Blog>
  2. {
  3. public override void Map(EntityTypeBuilder<Blog> b)
  4. {
  5. b.ToTable("Blog");
  6. b.HasKey(k => k.Id);
  7.  
  8. b.Property(p => p.Count);
  9. b.Property(p => p.Url);
  10. b.Property(p => p.Name);
  11.  
  12. b.Property(p => p.Code).IsRequired();
  13. //b.Property(p => p.BlogType).HasColumnType("TINYINT").IsRequired();
  14. }
  15. }

此时我们再来进行迁移如下:

  1. dotnet ef migrations add removeBlogType

此时将不会再报错且生成的removeBlogType迁移类文件如下:

  1. public partial class removeBlogType : Migration
  2. {
  3. protected override void Up(MigrationBuilder migrationBuilder)
  4. {
  5. migrationBuilder.DropColumn(
  6. name: "BlogType",
  7. table: "Blog");
  8. }
  9.  
  10. protected override void Down(MigrationBuilder migrationBuilder)
  11. {
  12. migrationBuilder.AddColumn<byte>(
  13. name: "BlogType",
  14. table: "Blog",
  15. type: "TINYINT",
  16. nullable: false,
  17. defaultValue: (byte));
  18. }
  19. }

我们需要再次确保生成的迁移类文件是否是我们需要修改的字段或者对列进行修改的方法是否正确,确保无误后,接下来再来通过 dotnet ef database update 更新到数据库中

如上通过意外情况导致上述错误,若是将最初迁移的整个文件夹删除了肿么办,这个时候真的没有好的办法了,我能想到的是:最好事先在建立项目时建立数据库对比文件,此时就会派上用场不用一个个类去核对表同时也利于部署时进行数据迁移。

总结

本文我们讲到了在EF Core迁移时可能出现的意外情况,若是删除了最初的迁移类文件也给出了能够想到的方案,不知看到此文的你有何高见?连续发表的EF Core文章都是在项目使用中遇到的问题,所以借此机会重新过了一遍,欢迎一起探讨。

EntityFramework Core迁移时出现数据库已存在对象问题解决方案的更多相关文章

  1. EntityFramework Core 迁移忽略主外键关系

    前言 本文来源于一位公众号童鞋私信我的问题,在我若加思索后给出了其中一种方案,在此之前我也思考过这个问题,借此机会我稍微看了下,目前能够想到的也只是本文所述方案. 为何要忽略主外键关系 我们不仅疑惑为 ...

  2. EntityFramework Core 运行dotnet ef命令迁移背后本质是什么?(EF Core迁移原理)

    前言 终于踏出第一步探索EF Core原理和本质,过程虽然比较漫长且枯燥乏味还得反复论证,其中滋味自知,EF Core的强大想必不用我再过多废话,有时候我们是否思考过背后到底做了些什么,到底怎么实现的 ...

  3. Cookies 初识 Dotnetspider EF 6.x、EF Core实现dynamic动态查询和EF Core注入多个上下文实例池你知道有什么问题? EntityFramework Core 运行dotnet ef命令迁移背后本质是什么?(EF Core迁移原理)

    Cookies   1.创建HttpCookies Cookie=new HttpCookies("CookieName");2.添加内容Cookie.Values.Add(&qu ...

  4. EntityFramework Core高并发深挖详解,一纸长文,你准备好了吗?

    前言 之前有关EF并发探讨过几次,但是呢,博主感觉还是有问题,为什么会觉得有问题,其实就是理解不够透彻罢了,于是在项目中都是用的存储过程或者SQL语句来实现,利用放假时间好好补补EF Core并发的问 ...

  5. EntityFramework Core并发深挖详解,一纸长文,你准备好看完了吗?

    前言 之前有关EF并发探讨过几次,但是呢,博主感觉还是有问题,为什么会觉得有问题,其实就是理解不够透彻罢了,于是在项目中都是用的存储过程或者SQL语句来实现,利用放假时间好好补补EF Core并发的问 ...

  6. EntityFramework Core 2.x (ef core) 在迁移中自动生成数据库表和列说明

    在项目开发中有没有用过拼音首字母做列名或者接手这样的项目? 看见xmspsqb(项目审批申请表)这种表名时是否有一种无法抑制的想肛了取名的老兄的冲动? 更坑爹的是这种数据库没有文档(或者文档老旧不堪早 ...

  7. 在.NET Core类库中使用EF Core迁移数据库到SQL Server

    前言 如果大家刚使用EntityFramework Core作为ORM框架的话,想必都会遇到数据库迁移的一些问题. 起初我是在ASP.NET Core的Web项目中进行的,但后来发现放在此处并不是很合 ...

  8. ABP .Net Core Entity Framework迁移使用MySql数据库

    一.迁移说明 ABP模板项目Entity Framework Core默认使用的是Sql Server,也很容易将数据库迁移到MySQL,步骤如下. 二.迁移MySQL步骤 1. 下载项目 请到 ht ...

  9. ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core

    前言 原本本节内容是不存在的,出于有几个人问到了我:我想使用ASP.NET Core Identity,但是我又不想使用默认生成的数据库表,想自定义一套,我想要使用ASP.NE Core Identi ...

随机推荐

  1. CodeVS1344 线型网络

    题目描述 Description 有 N ( <=20 ) 台 PC 放在机房内,现在要求由你选定一台 PC,用共 N-1 条网线从这台机器开始一台接一台地依次连接他们,最后接到哪个以及连接的顺 ...

  2. .net之抽象工厂模式

    //抽象工厂 //抽象食物 namespace abstractFactory{ public abstract class food { public abstract void Food(); } ...

  3. selenium+python

    最近在学习selenium自动化测试,但是一直遇到一个问题,总是打不开指定的网址,今天突然成功了, 主要原因是因为selenium版本太低的缘故,所以只需要在终端输入:pip install -U s ...

  4. 利用Sinopia搭建私有npm包

    1.安装sinopia包 npm install -g sinopia 如果是Windows系统用上面的方式安装sinopia很有可能报错,推荐使用下面方式安装: npm install sinopi ...

  5. Python输入一个数字打印等腰三角形

    要求 用户输入一个数字,按照数字打印出等腰三角形 思路 1,用户输入的数字为n代表一共有多少行 2,使用一个循环带两个for循环,第一层循环是循环行数,第二层两个平行for循环一个打印空格一个打印*号 ...

  6. Ubuntu安装中文输入法

    如果你没有因为各种尝试搞乱了Ubuntu系统的话: 1. 在All Settings 里找到 Language Support, Install/Remove Languages里安装Chinese ...

  7. http协议中:GET/POST/PUT/DELETE/TRACE/OPTIONS/HEAD方法

    ###1 HTTP/1.1协议中共定义了八种方法(有时也叫"动作")来表明Request-URI指定的资源的不同操作方式: OPTIONS 返回服务器针对特定资源所支持的HTTP请 ...

  8. Longest Substring Without Repeating Characters2015年6月9日

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. Visual Studio Code for mac

    Visual Studio Code for mac 将下载文件解压拖到应用程序文件夹即可 下载地址:链接: https://pan.baidu.com/s/1geHL5f1 密码: 2fdw

  10. 最简单 iText 的 PDF 生成方案(含中文解决方案)HTML 转为 PDF

    转自:http://my.oschina.net/sanji/blog/277704 最近正好项目有用到 ITEXT ,在网络上搜索了一番,发现了很多方案,但是感觉对于一般开发来说都太复杂了,本文提供 ...