简体   繁体   English

如何在 Entity Framwowrk Core 中为代码优先方法创建一个新表

[英]How to create a new table in Entity Framwowrk Core for code-first approach

Initially I started with the below code最初我从下面的代码开始

public class ShareMyBlogPostContext : DbContext
{
   public ShareMyBlogPostContext(DbContextOptions<ShareMyBlogPostContext> options):base(options)
   {
   }

   public DbSet<User> Users { get; set; }
}

And I ran the below commands to create the database table and migrations:我运行以下命令来创建数据库表和迁移:

dotnet ef migrations add InitialDb
dotnet ef database update

But after some time, I realized that I need to have a new entity and I updated my DbContext class as below:但过了一段时间,我意识到我需要一个新实体,我更新了我的DbContext class 如下:

public class ShareMyBlogPostContext : DbContext
{
   public ShareMyBlogPostContext(DbContextOptions<ShareMyBlogPostContext> options) : base(options)
   { }

   public DbSet<User> Users { get; set; }
   public DbSet<Blog> Blogs { get; set; }
}

Since I was just starting out the project and there was not much data in Users table, it was not a big task for me to delete the already existing Users table from the database and Migrations folder from my project, and re-executing the above commands.由于我刚刚开始项目并且Users表中的数据不多,因此从我的项目中删除数据库中已经存在的Users表和Migrations文件夹并重新执行上述命令对我来说并不是什么大问题. This way I was able to create a new database table named Blogs .这样我就能够创建一个名为Blogs的新数据库表。

But how to create a new database table of the newly added entity in the DbContext without removing the existing tables and migrations?但是如何在不删除现有表和迁移的情况下,在DbContext中创建新添加实体的新数据库表? As this would not be the scenario every time.因为这不会是每次的情况。

hi please type this command嗨,请输入此命令

dotnet ef migrations add AddBlogs
dotnet ef database update

for more please visit this Page更多信息请访问此页面

also if you want to keep your table you must be change migration.cs files manual to add your tables and fields and just run this command此外,如果你想保留你的表,你必须更改 migration.cs 文件手册来添加你的表和字段,然后运行这个命令

dotnet ef database update

EF always looks inside DbContext derived class ( ShareMyBlogPostContext in your case) to detect changes. EF 始终查看DbContext派生的ShareMyBlogPostContext (在您的情况下为 ShareMyBlogPostContext )以检测更改。 Sometimes you may not add separate DbSet instance for a newely created class, never mind, if you make a relationship between this newly created class and one of DbSet classes, the required tables will be appeared just after you request it .有时您可能不会为新创建的 class 添加单独的DbSet实例,没关系,如果您在此新创建的 class 和其中一个DbSet类之间建立关系,则在您请求后将出现所需的表。 Every thing in EF codefirst is fully-managed and nothing will happen unless you run migrations add migrationName (.net CLI command) in order to generate script for current changes. EF codefirst 中的所有内容都是完全托管的,除非您运行migrations add migrationName (.net CLI 命令)以便为当前更改生成脚本,否则不会发生任何事情。 Based on your configuration , there may no migration to be applied unless you request it by database update .根据您的配置,除非您通过database update请求,否则可能不会应用迁移。 In this way, you may have multiple migrations, the command Updates the database schema based on the last migration snapshot .这样,你可能会有多次迁移,命令 Updates the database schema based on last migration snapshot

Dealing with database changes is a critical parts of any project.处理数据库更改是任何项目的关键部分。 So, please read EF document before.所以,请先阅读EF 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Entity Framework Core 2.2.3不使用代码优先方法创建表与2017年相比 - Entity Framework Core 2.2.3 not creating table using code-first approach vs 2017 Entity Framework Core 2.1,使用代码优先迁移重命名表而不删除和创建新表 - Entity Framework Core 2.1, rename table using code-first migrations without dropping and creating new table 如何从实体框架代码优先方法自动增加 oracle 表? - How to auto-increment oracle table from Entity Framework code-first approach? 实体未使用“代码优先”方法更新 - Entity not updating using Code-First approach 使用实体框架Code-First创建表,运行时 - Create Table, Run Time using entity framework Code-First 如何在Entity Framework Code-First中创建自定义M2M表 - How can I create a custom m2m table in Entity Framework Code-First 实体框架核心计数表和记录代码优先方法(MySQL) - Entity Framework Core counting tables and records Code-First Approach (MySQL) 如何在实体的使用表上创建PK和唯一用户名(使用代码优先)? - How to create PK and Unique Username at Use Table in Entity (Using Code-first)? 如何在实体框架代码优先中创建具有自引用的模型? - How to create a model with self reference in Entity Framework Code-First? 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM