【问题标题】:.NET Core creating non-nullable FK constraint with IdentityUser.NET Core 使用 IdentityUser 创建不可为空的 FK 约束
【发布时间】:2019-01-28 04:14:20
【问题描述】:

我正在尝试使用 .NET Core 2.2 中的 EF 迁移创建从我的主要 IdentityUser 到另一个类的关系。我遇到了一个问题,那就是正在使用 null 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);
            });

我现在得到了正确的 non-nullable 约束,但它是重复的。 (有一个 1 附加)。我的问题是:

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

【问题讨论】:

    标签: c# entity-framework asp.net-core asp.net-core-mvc


    【解决方案1】:

    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; }
    } 
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2021-09-10
        • 1970-01-01
        • 2022-08-23
        • 2019-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-24
        相关资源
        最近更新 更多