如果在此之前使用EF代码优先方法并执行CRUD操作,如何将更多表添加到数据库中?

问题描述 投票:0回答:1

我通过使用EF代码优先的方法从ASP.NET Core Web API项目在SSMS中创建了一个数据库,效果很好。

现在,我需要向数据库中再添加一个表,并将表中的主键用作外键并执行CURD操作。或共享包含类似示例或youtube链接的网站。

模型类:

namespace WebApi.Models
{
    public class ApplicationUser : IdentityUser
    {
        public string CNIC { get; set; }

        public string FullName { get; set; }
    }

}

执行“初始迁移”和“更新数据库”后创建了多个表

migrationBuilder.CreateTable(
                name: "AspNetUsers",
                columns: table => new
                {
                    Id = table.Column<string>(nullable: false),
                    UserName = table.Column<string>(maxLength: 256, nullable: true),
                    NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
                    Email = table.Column<string>(maxLength: 256, nullable: true),
                    NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
                    EmailConfirmed = table.Column<bool>(nullable: false),
                    PasswordHash = table.Column<string>(nullable: true),
                    SecurityStamp = table.Column<string>(nullable: true),
                    ConcurrencyStamp = table.Column<string>(nullable: true),
                    PhoneNumber = table.Column<string>(nullable: true),
                    PhoneNumberConfirmed = table.Column<bool>(nullable: false),
                    TwoFactorEnabled = table.Column<bool>(nullable: false),
                    LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
                    LockoutEnabled = table.Column<bool>(nullable: false),
                    AccessFailedCount = table.Column<int>(nullable: false),
                    Discriminator = table.Column<string>(nullable: false),
                    CNIC = table.Column<string>(nullable: true),
                    FullName = table.Column<string>(nullable: true)
                },

我想使用上表的ID作为表中的外键并执行CRUD操作。

我不知道如何执行此操作。

c# entity-framework-core asp.net-core-webapi
1个回答
0
投票

如下所示向新表的模型类添加新属性

public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual AspNetUsers AspNetUser { get; set; }
© www.soinside.com 2019 - 2024. All rights reserved.