使用Identity将新表添加到ASP .NET Core MVC应用程序中(Add a new table into the ASP .NET Core MVC app with Identity)

编程入门 行业动态 更新时间:2024-10-27 06:27:54
使用Identity将新表添加到ASP .NET Core MVC应用程序中(Add a new table into the ASP .NET Core MVC app with Identity)

我有一个关于在ASP.NET Core MVC应用程序中创建新表的问题。 我用“个人用户帐户”创建了它,所以我在DB中有身份表。

如何在此DB中创建自己的表? 我应该创建一个新的DB上下文吗?

例如:我想为学生和问题(及其答案)创建一个表格。

I have a question about creating a new table in ASP.NET Core MVC application. I've created it with "Individual User Accounts" so I have Identity tables in DB.

How can I create my own tables in this DB? Should I create a new DB Context?

For example: I want to create a tables for students and questions(and their answers).

最满意答案

由于您已经说过使用了“ Individual User Accounts身份”模板,因此您只需在其上添加自定义模型,例如:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); //.... } public DbSet<Student> Students{ get; set; } public DbSet<Question> Questions { get; set; } public DbSet<Answer> Answers { get; set; } //.... }

在新更改后添加/运行迁移 - 从“工具”菜单中找到Nuget Console Manager Package Console Manager下的“ Package Console Manager Nuget Console Manager 。 使用ApplicationDbContext选择项目并运行屏幕截图中显示的命令。

在此处输入图像描述

Since you already said you used the Individual User Accounts Identity template, you can just add your custom models on it, something like:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); //.... } public DbSet<Student> Students{ get; set; } public DbSet<Question> Questions { get; set; } public DbSet<Answer> Answers { get; set; } //.... }

To add/run migrations after the new changes -- From the Tools Menu locate Package Console Manager under Nuget Console Manager. Select the project with your ApplicationDbContext and run the commands as shown in the screenshot.

enter image description here

更多推荐

本文发布于:2023-08-04 08:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   NET   ASP   Identity   将新表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!