简体   繁体   English

如何使用 EF Core 迁移在我的数据库中创建多个表

[英]How can I create many tables in my database using EF Core migrations

I am doing a bookstore project and I first created one table for the adding book.我正在做一个书店项目,我首先为添加书籍创建了一个表。 So I want to add login and signup pages and store to the database, but I am confused about how I can add another table or create tables related to my need using migrations.因此,我想添加登录和注册页面并将其存储到数据库中,但我对如何使用迁移添加另一个表或创建与我的需求相关的表感到困惑。 I have attached my DbContext class.我附上了我的DbContext类。

Forgive me my English is not so good.原谅我的英语不是很好。 I am waiting for your answers.我在等你的答案。 Thanks谢谢

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CODEwithZAKI.Data
{
    public class BookStoreContext : DbContext
    {
        public BookStoreContext(DbContextOptions<BookStoreContext> options)
            : base(options)
        {
        }

        public DbSet<Books> Books { get; set; }
    }
}

Dbcontext Class数据库上下文类

For how to add a new table to the database with ef core code first, you can follow the below steps:关于如何先使用ef核心代码向数据库添加新表,可以按照以下步骤操作:

1.Create your new table as a model 1.创建您的新表作为模型

public class Author
{
    public int Id { get; set; }

    public string Name { get; set; }

    //other properties
}

2.Add its entry in DbContext class 2.在DbContext类中添加其条目

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

    public DbSet<Books> Books { get; set; }

    public DbSet<Author> Authors { get; set; }
}

3.Create a new migration with the addition of Posts in Package Manager Console 3.在包管理器控制台中添加帖子创建一个新的迁移

Add-Migration AuthorMigration

4.Update database 4.更新数据库

Update-Database

To add new tables to you database using migrations is simply a matter of extending your BookStoreContext with the new sets and then running the migration commands.使用迁移向数据库添加新表只需使用新集合扩展BookStoreContext ,然后运行迁移命令即可。 As an example using dotnet command.作为使用dotnet命令的示例。

Generate the new migration script:生成新的迁移脚本:

dotnet ef migrations add DESCRIPTION_OF_YOUR_MIGRATION

Run the migration:运行迁移:

dotnet ef database update

And that's it.就是这样。 Everytime you add new records to your BookStoreContext you just go through the above two commands to run the EF Core migration process.每次向 BookStoreContext 添加新记录时,您只需通过上述两个命令来运行 EF Core 迁移过程。

PS For login/authentication/identity it is recommended to utilize ASP.NET Core Identity . PS 对于登录/身份验证/身份,建议使用ASP.NET Core Identity

暂无
暂无

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

相关问题 如何在运行时读取迁移文件并将它们应用到 EF Core 中的数据库? - How can I read migrations files at runtime and apply them to my database in EF Core? EF Core 数据库迁移 - EF Core database migrations 如何使用 .NET Core EF 即时创建数据库 - How can I create a database on the fly with .NET Core EF 如何通过代码直接使用 EF Core Migrations API - How can I use EF Core Migrations API directly by code EF Core 3.1:删除了所有迁移和数据库,如何在新数据库中重新创建所有表? - EF Core 3.1: Deleted all Migrations and the Database, how to recreate all tables in a new database? 如何卸载 EF Core 表? 或者如何删除所有迁移? 或者如何从用户代码调用`dotnet ef database update 0`? - How to uninstall EF Core tables? Or how to remove all migrations ? Or how to call `dotnet ef database update 0` from user code? ASP.NET MVC EF-Migrations - 如何使用表和所有已保存的记录从dataBase创建SCRIPT - ASP.NET MVC EF-Migrations - How to create a SCRIPT from the dataBase with tables and all saved records 如何使用数据库迁移和EF 4.3与代码第一种方法来创建关系表? - How to use Database Migrations and EF 4.3 with a code first approach to create relationship tables? 我可以在EF core asp.net中使用相同模型创建多个表吗 - Can i create multiple tables using same model in EF core asp.net 为什么我不能为我的 EF Core 迁移和更新指定连接字符串? - Why can't I specify the connection string for my EF Core migrations and update?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM