首页 > 其他分享 >EF Core 的基本使用

EF Core 的基本使用

时间:2023-09-03 16:34:12浏览次数:34  
标签:基本 Core ef EF EntityFrameworkCore dotnet table 迁移 Microsoft

基本命令

EF Core的迁移(Migration)是一种用于管理数据库架构变化的功能,它可以根据你的数据模型自动生成和执行数据库创建或者更新的SQL语句。EF Core提供了一些命令行工具,让你可以方便地操作迁移。以下是一些常用的迁移命令:

  • dotnet ef migrations add <name>: 这个命令用于创建一个新的迁移,它会根据你的数据模型和当前的数据库架构,生成一个包含SQL语句的C#类,用于更新数据库。你需要给这个迁移一个有意义的名字,例如InitialCreate或者AddStudentAge。这个命令会在你的项目中创建一个名为Migrations的文件夹,用于存放所有的迁移类。
  • dotnet ef migrations remove: 这个命令用于删除最近添加的迁移,它会从你的项目中删除对应的迁移类。如果你想删除一个旧的或者中间的迁移,你需要先回滚到那个迁移之前,然后再删除它,再重新添加后面的迁移。
  • dotnet ef migrations list: 这个命令用于列出所有已经添加的迁移,它会按照时间顺序显示迁移的名字和ID。
  • dotnet ef database update: 这个命令用于更新数据库,它会执行最新的迁移类中的SQL语句,把数据库架构变更为和数据模型一致。如果你没有指定任何参数,它会更新到最新的迁移。如果你指定了一个迁移的名字或者ID,它会更新到那个迁移。如果你指定了0作为参数,它会回滚到空数据库。
  • dotnet ef database drop: 这个命令用于删除数据库,它会把整个数据库文件或者表删除。这个命令需要谨慎使用,因为它会丢失所有的数据。

删除迁移失败的原因

我很抱歉你遇到了错误。根据你的输出,我发现你的错误是因为你使用了错误的命令来删除迁移。你不能在dotnet ef migrations remove这个命令后面加上迁移的名字或者ID,这个命令只能删除最近添加的迁移。如果你想删除一个旧的或者中间的迁移,你需要先回滚到那个迁移之前,然后再删除它,再重新添加后面的迁移。

如果你想删除20230903032747_set villa table UpdatedDate is nullable这个迁移,你可以执行以下命令:

dotnet ef database update 20230902135255_create villa table
dotnet ef migrations remove

删除过程记录

列出所有的迁移

song@songdembp@ MagicVilla_VillaAPI % dotnet ef migrations list
Build started...
Build succeeded.
The Entity Framework tools version '7.0.9' is older than that of the runtime '7.0.10'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT "MigrationId", "ProductVersion"
      FROM "__EFMigrationsHistory"
      ORDER BY "MigrationId";
20230902135255_create villa table
20230903032747_set villa table UpdatedDate is nullable

删除迁移

删除失败

song@songdembp@ MagicVilla_VillaAPI % dotnet ef database update 20230903032747
Build started...
Build succeeded.
The Entity Framework tools version '7.0.9' is older than that of the runtime '7.0.10'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT "MigrationId", "ProductVersion"
      FROM "__EFMigrationsHistory"
      ORDER BY "MigrationId";
System.InvalidOperationException: The migration '20230903032747' was not found.
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsAssemblyExtensions.GetMigrationId(IMigrationsAssembly assembly, String nameOrId)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.PopulateMigrations(IEnumerable`1 appliedMigrationEntries, String targetMigration, IReadOnlyList`1& migrationsToApply, IReadOnlyList`1& migrationsToRevert, Migration& actualTargetMigration)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GetMigrationCommandLists(IReadOnlyList`1 appliedMigrationEntries, String targetMigration)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The migration '20230903032747' was not found.

删除成功(要给全称)

song@songdembp@ MagicVilla_VillaAPI % dotnet ef database update "20230902135255_create villa table"

Build started...
Build succeeded.
The Entity Framework tools version '7.0.9' is older than that of the runtime '7.0.10'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
..............................

查看删除之后的迁移

  1. 有个Pending中
song@songdembp@ MagicVilla_VillaAPI % dotnet ef migrations list
Build started...
Build succeeded.
The Entity Framework tools version '7.0.9' is older than that of the runtime '7.0.10'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT "MigrationId", "ProductVersion"
      FROM "__EFMigrationsHistory"
      ORDER BY "MigrationId";
20230902135255_create villa table
20230903032747_set villa table UpdatedDate is nullable (Pending)

删除哪个Pending迁移

  1. 当然也可以手动去文件夹中删除
song@songdembp@ MagicVilla_VillaAPI % dotnet migrations remove 
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET program, but dotnet-migrations does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
song@songdembp@ MagicVilla_VillaAPI % dotnet ef migrations remove
Build started...
Build succeeded.
The Entity Framework tools version '7.0.9' is older than that of the runtime '7.0.10'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT "MigrationId", "ProductVersion"
      FROM "__EFMigrationsHistory"
      ORDER BY "MigrationId";
Removing migration '20230903032747_set villa table UpdatedDate is nullable'.
Reverting the model snapshot.
Done.

剩下的迁移

song@songdembp@ MagicVilla_VillaAPI % dotnet ef migrations list
Build started...
Build succeeded.
The Entity Framework tools version '7.0.9' is older than that of the runtime '7.0.10'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT "MigrationId", "ProductVersion"
      FROM "__EFMigrationsHistory"
      ORDER BY "MigrationId";
20230902135255_create villa table

标签:基本,Core,ef,EF,EntityFrameworkCore,dotnet,table,迁移,Microsoft
From: https://www.cnblogs.com/zhuoss/p/17675138.html

相关文章

  • css: A Multi-line CSS only Typewriter effect
     <!doctypehtml><html><head><metacharset="utf-8"><metaname="viewport"content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"><metahttp-eq......
  • Unity业务抽象套路二、EIP Everythings Is Prefab
     为什一些控制、数据管理的逻辑也要做成Prefab?好处:可以在Inspector中调整参数(而不是散落在各个配置文件中)调试时能够在Inspector确认具体数值自然地支持一系列方法:携程、定时、Update、FixedUpDate注意:有人习惯将配置写成ScriptableObject然后统一以此来管理。个人建......
  • Linux系统之ifconfig命令的基本使用
    (Linux系统之ifconfig命令的基本使用)一、ifconfig命令介绍1.ifconfig简介ifconfig是Linux中常用的网络配置工具之一,用于配置和显示网络接口的具体状况。2.ifconfig注意事项用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的......
  • Spring-webflux简介及基本使用
    spring-webflux是spring在5.0版本后提供的一套响应式编程风格的web开发框架。这个框架包含了spring-framework和springmvc,它可以运行在Netty、Undertow以及3.1版本以上的Serlvet容器上。你可以在项目中同时使用spring-webmvc和spring-webflux,或者只用其中一个来开发web应用。......
  • 扩容Linux文件系统:从基本原理到实践
    一、引言在Linux系统中,文件系统是存储和组织数据的核心组件。随着应用程序和数据的不断增加,有时候需要扩大文件系统的容量。本文将介绍扩容Linux文件系统的方法和步骤,帮助您轻松应对存储需求。二、准备步骤在进行文件系统扩容之前,需要确保以下事项:了解现有磁盘空间:使用df-h命令查......
  • Educational Codeforces Round 154 (Rated for Div. 2)
    Preface太FW了现在,纯纯给队伍拖后腿,马上要成为我们队CFRating最低的了但换句话说徐神和祁神都这么猛,我直接躺着被嘎嘎带飞好像也很爽啊不管怎么样还是要多练,不过接下来可能要按专题重点突破了,明天队里开个会确定下大家的主攻方向再说A.PrimeDeletion因为\(13\)和\(31\)都......
  • C++ Core Guidelines解析 电子书 pdf
    关注公众号:红宸笑。回复:电子书即可  在《C++CoreGuidelines解析》中,C++专家讲师RainerGrimm提炼出了CoreGuidelines中的精髓,去除了晦涩难懂的内容,分享了新的见解和背景,并提供了自己培训课程中经过充分测试的示例。对于使用C++11及后续版本C++的有经验程序员,G......
  • MySQL的基本语句
    目录1.0数据库操作2.0数据表操作2.1表的创建2.2表的修改2.2.1表中字段的添加2.2.2表中字段的修改2.2.3表中字段的删除2.3表的查询2.3.1查询数据库中所有的表2.3.2查询表结构2.4表的删除2.5表中数据的操作2.5.1表数据的查询2.5.1.1表中数据的简单查询2.5.1.2表中数据......
  • Educational Codeforces Round 23 A - F
    EducationalCodeforcesRound23目录EducationalCodeforcesRound23A-TreasureHuntB-MakesAndTheProductC-ReallyBigNumbersD-ImbalancedArrayE-ChoosingTheCommanderF-MEXQueriesA-TreasureHunt往四个方向走,每次操作会变动横坐标和纵坐标,横纵坐标......
  • nner join 与 left join 之间的区别
    nnerjoin与leftjoin之间的区别关于innerjoin与leftjoin之间的区别,以前以为自己搞懂了,今天从前端取参数的时候发现不是预想中的结果,才知道问题出在innerjoin上了。需求是从数据库查数据,在前端以柱形图的形式展现出来,查到的数据按行业分组,显示每个行业的户数及户数占比,涉......