繁体   English   中英

.NET Core 使用 IdentityUser 创建不可为空的 FK 约束

[英].NET Core creating non-nullable FK constraint with IdentityUser

我正在尝试使用 .NET Core 2.2 中的 EF 迁移创建从我的主 IdentityUser 到另一个类的关系。 我遇到了一个问题,那就是正在使用FK 约束创建迁移。 这是我不想要的。

我担心,我不应该手动编辑迁移文件(以解决此问题),因为当我将来更改这些模型时,我的手动编辑将被覆盖(因为我的类以某种方式编码不正确)。

这些是我的课..

public class ApplicationUser : IdentityUser
{
    public List<Purchase> Purchases { get; set; }
}

public class Purchase
{
    public int Id { get; set; }
    // others
}

生成的迁移文件在 ApplicationUserId 上设置了一个nullable: true属性

migrationBuilder.CreateTable(
            name: "Purchase",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                ApplicationUserId = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Purchase", x => x.Id);
                table.ForeignKey(
                    name: "FK_Purchase_AspNetUsers_ApplicationUserId",
                    column: x => x.ApplicationUserId,
                    principalTable: "AspNetUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

不正确。 因此,我在Purchase类中添加了属性(就像我在项目中的其他 POCO 中一样)。

public class Purchase
{
    public int Id { get; set; }
    // others

    public int ApplicationUserId { get; set; }
    public ApplicationUser ApplicationUser { get; set; }
}    

并重新运行迁移重新生成..

migrationBuilder.CreateTable(
            name: "Purchase",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                ApplicationUserId = table.Column<int>(nullable: false),
                ApplicationUserId1 = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Purchase", x => x.Id);
                table.ForeignKey(
                    name: "FK_Purchase_AspNetUsers_ApplicationUserId1",
                    column: x => x.ApplicationUserId1,
                    principalTable: "AspNetUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

我现在得到了正确的不可为空约束,但它是重复的。 (有一个 1 钉)。 我的问题是:

  1. 简单地修改迁移文件并收工对我来说安全吗?
  2. 如何在ApplicationUserPurchase类之间创建关系,以便生成的迁移文件为我提供不可为空的 FK 约束?

1) 您可以,但每次后续迁移都会重新创建此字段并导致您遇到问题。 此外,由于数据类型不匹配,它不会按预期工作

2)外键的id类型错误。 应用程序用户有一个string类型的主键,但您使用的是int因此无法识别正确的外键。

更正后,将Required属性放在类上。 所以最后的课程看起来像

using System.ComponentModel.DataAnnotations;

public class Purchase
{
    public int Id { get; set; }
    // others

    [Required]
    public string ApplicationUserId { get; set; }
    public ApplicationUser ApplicationUser { get; set; }
} 

您可以使用Add-Migration命令生成迁移脚本,之后您可以编辑迁移源代码并在创建新表后立即添加至少 1 条记录:

migrationBuilder.InsertData(
    table: "Organizations",
    columns: new[] { "OrganizationName", "IsDeleted" },
    values: new object[,]
    {
        { "Test organization", false }
    });

之后,您应该能够管理新外键字段的创建,将 1 作为默认值。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM