c# - .NET Core 使用 IdentityUser 创建不可为 null 的 FK 约束

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

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

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

这些是我的类(class)..

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 类之间创建关系,以便生成的迁移文件为我提供不可为 null 的 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; }
} 

关于c# - .NET Core 使用 IdentityUser 创建不可为 null 的 FK 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395460/

相关文章:

c# - 在最终不需要计算器的情况下将枚举定义为 2 的标志/幂的任何技巧?

c# - WSDL 生成的代码未正确反序列化 XML 中的 long

c# - 最佳实践 : Persisting data throughout multiple web method calls

.net - 数据库架构更改是否会破坏实体的 linq

c# - int 和 string 之间的唯一索引

c# - VS2017 - 缺少 .net 核心 > 2.1 作为目标框架

c# - 公开 .Net 服务

c# - 身份的双重外键困境

c# - 访问静态类的配置/设置 - Asp Core

c# - 在 ASP.NET Core MVC 中为客户端验证配置区域性