[英]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.