前言

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

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

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

Add-Migration Initial -IgnoreChanges

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

dotnet ef migrations add <<migration_name>>
dotnet ef database update

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

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

There is already an object named in the database

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

    public partial class initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Blog",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Code = table.Column<string>(nullable: false),
Count = table.Column<int>(nullable: false),
Name = table.Column<string>(nullable: true),
Url = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Blog", x => x.Id);
}); migrationBuilder.CreateTable(
name: "Book",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Book", x => x.Id);
}); migrationBuilder.CreateTable(
name: "Category",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true),
ProductId = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Category", x => x.Id);
}); migrationBuilder.CreateTable(
name: "Product",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Code = table.Column<string>(nullable: true),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Product", x => x.Id);
}); migrationBuilder.CreateTable(
name: "Post",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
BlogId = table.Column<int>(nullable: false),
Content = table.Column<string>(nullable: true),
Title = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Post", x => x.Id);
table.ForeignKey(
name: "FK_Post_Blog_BlogId",
column: x => x.BlogId,
principalTable: "Blog",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}); migrationBuilder.CreateTable(
name: "ProductCategory",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
CategoryId = table.Column<int>(nullable: false),
ProductId = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ProductCategory", x => x.Id);
table.ForeignKey(
name: "FK_ProductCategory_Category_CategoryId",
column: x => x.CategoryId,
principalTable: "Category",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ProductCategory_Product_ProductId",
column: x => x.ProductId,
principalTable: "Product",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}); migrationBuilder.CreateIndex(
name: "IX_Post_BlogId",
table: "Post",
column: "BlogId"); migrationBuilder.CreateIndex(
name: "IX_ProductCategory_CategoryId",
table: "ProductCategory",
column: "CategoryId"); migrationBuilder.CreateIndex(
name: "IX_ProductCategory_ProductId",
table: "ProductCategory",
column: "ProductId");
} protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Book"); migrationBuilder.DropTable(
name: "Post"); migrationBuilder.DropTable(
name: "ProductCategory"); migrationBuilder.DropTable(
name: "Blog"); migrationBuilder.DropTable(
name: "Category"); migrationBuilder.DropTable(
name: "Product");
}

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

    public partial class initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{} protected override void Down(MigrationBuilder migrationBuilder)
{
........
}
}

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

    public class BlogMap : EntityMappingConfiguration<Blog>
{
public override void Map(EntityTypeBuilder<Blog> b)
{
b.ToTable("Blog");
b.HasKey(k => k.Id); b.Property(p => p.Count);
b.Property(p => p.Url);
b.Property(p => p.Name); b.Property(p => p.Code).IsRequired();
//b.Property(p => p.BlogType).HasColumnType("TINYINT").IsRequired();
}
}

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

dotnet ef migrations add removeBlogType

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

    public partial class removeBlogType : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "BlogType",
table: "Blog");
} protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<byte>(
name: "BlogType",
table: "Blog",
type: "TINYINT",
nullable: false,
defaultValue: (byte));
}
}

我们需要再次确保生成的迁移类文件是否是我们需要修改的字段或者对列进行修改的方法是否正确,确保无误后,接下来再来通过 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. 关于使用mybatis的几点总结

    mybatis的几点总结 1.关于resultType和resultMap的使用 查询结果集统一采用resultType,不要采用resultMap,因为resultMap需要写许多的表字段和实体类的 ...

  2. [ext4]06 磁盘布局 - 特殊inode

    Ext4预留了一些inode做特殊特性使用,见下表: inode Purpose 0 不存在,Ext4中不存在inode 0. 1 存放损坏的数据块链表 2 根目录 3 User quota. 用户q ...

  3. vsftp使用方法与问题解决

    安装环境 OS:Centos 6.4 vsftp:vsftpd-2.2.2-11.el6_3.1.i686.rpm vsftpd配置文件:/etc/vsftpd/vsftpd.conf 一.      ...

  4. PHP学习笔记-3

    PHP 数据类型: 字符串.整数.浮点数.逻辑.数组.对象.NULL. JavaScript数据类型: 字符串.数字.布尔.数组.对象.Null.Undefined. 从上面可以看出来,数据类型都是7 ...

  5. VS2015如何新建C++或者C语言版的lib文件

    当我们不想公开我们的代码的时候,可以把我们的代码封装成静态数据连接库,即lib文件.下面介绍下如何生成lib文件. 以VS2015为例,一种是C++版的lib文件,一种是C语言版的lib文件. 一.按 ...

  6. luogu 1521-求逆序对

    题意: 逆序对指在一个序列中ai>aj && i < j,也就是一前一后两个数,当大的在前面的时候即算一对. 题目求在一个由1-n组成的序列中逆序对为k的序列的个数. 出题 ...

  7. O(nlogn)实现LCS与LIS

    序: LIS与LCS分别是求一个序列的最长不下降序列序列与两个序列的最长公共子序列. 朴素法都可以以O(n^2)实现. LCS借助LIS实现O(nlogn)的复杂度,而LIS则是通过二分搜索将复杂度从 ...

  8. .net 程序集

    前言:用了几天的时间把高级编程里面程序集一章看完了,原来自己只知道写代码,右键添加引用,从来也不知道操作的实质是什么,微软总是这个套路,鼠标点点就能把任务完成,这对新手友好但是对要通透了解程序执行和内 ...

  9. Firefox实用插件记录

    之前总结过一个软件推荐的小文,用来记录一直以来在软件开发过程中遇到的各种实用的软件.后来发现里面越来越多的记录了Firefox的插件,所以今天决定单独抽出一个页面来记录Firefox的插件.因为平时大 ...

  10. 使用CSS和JQuery实现表格单元格内容超出时部分隐藏,隐藏部分以...显示

    1.使用CSS实现,给此单元格添加一个Class: width:130px; display:block; overflow:hidden; word-break:keep-all; white-sp ...