我有一个关于在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.
更多推荐
发布评论