首页 文章

在ApplicationDBContext中将Dot Net Core Identity User引用为Foreign

提问于
浏览
0

我正在使用Identity DB上下文和Application DB上下文,我有一个表(UserVehicles),其中我使用ApplicationUser(Identity User)属性进行外键引用,当我为ApplicationDBContext添加迁移时,它正在创建新的脚本表“ApplicationUser”并将外键关系添加到新表而不是默认的AspnetUsers(身份用户)表 . 以下是课程

IdentityDBContext类

public class ApplicationUser : IdentityUser<int>
{
    [PersonalData]
    public string FirstName { get; set; }
    [PersonalData]
    public string LastName { get; set; }
}

ApplicationDBContext类

[Table("UserVehicles")]
public class UserVehicle
{
    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "Name is required.")]
    [MaxLength(50, ErrorMessage = "Name maximum length is 50.")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Tracker unique id is required.")]
    public string TrackerUniqueId { get; set; }

    [Required(ErrorMessage = "Server host is required.")]
    public string ServerHost { get; set; }

    public bool IsActive { get; set; }

    [Required(ErrorMessage = "User is required.")]
    [ForeignKey("ApplicationUser")]
    public int UserId { get; set; }

    public virtual ApplicationUser ApplicationUser { get; set; }
}

下面是创建的迁移

protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "ApplicationUser",
            columns: table => new
            {
                AccessFailedCount = table.Column<int>(nullable: false),
                EmailConfirmed = table.Column<bool>(nullable: false),
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                LockoutEnabled = table.Column<bool>(nullable: false),
                LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
                PhoneNumberConfirmed = table.Column<bool>(nullable: false),
                TwoFactorEnabled = table.Column<bool>(nullable: false),
                UserName = table.Column<string>(nullable: true),
                NormalizedUserName = table.Column<string>(nullable: true),
                Email = table.Column<string>(nullable: true),
                NormalizedEmail = table.Column<string>(nullable: true),
                PasswordHash = table.Column<string>(nullable: true),
                SecurityStamp = table.Column<string>(nullable: true),
                ConcurrencyStamp = table.Column<string>(nullable: true),
                PhoneNumber = table.Column<string>(nullable: true),
                FirstName = table.Column<string>(nullable: true),
                LastName = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_ApplicationUser", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "UserVehicles",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                Name = table.Column<string>(maxLength: 50, nullable: false),
                TrackerUniqueId = table.Column<string>(nullable: false),
                ServerHost = table.Column<string>(nullable: false),
                IsActive = table.Column<bool>(nullable: false),
                UserId = table.Column<int>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_UserVehicles", x => x.Id);
                table.ForeignKey(
                    name: "FK_UserVehicles_ApplicationUser_UserId",
                    column: x => x.UserId,
                    principalTable: "ApplicationUser",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });
    }

我试图在迁移类中删除ApplicationUser的代码也将表名更新为Identity Users Table名并更新数据库然后当我在UserVehicles表上查询时,我得到“ApplicationUser表不存在”

1 回答

  • 0

    我曾经遇到过同样的问题,但另一方面也有导航属性 . 我使用了 InverseProperty 属性的决心 .

    也许这会奏效 .

    public class ApplicationUser : IdentityUser<int>
    {
        /// Your others props...
    
        public virtual ICollection<UserVehicle> Vehicles { get; set; }
    }
    
    [Table("UserVehicles")]
    public class UserVehicle
    {
        /// Your others props...
    
        [InverseProperty("Vehicles")]
        public virtual ApplicationUser ApplicationUser { get; set; }
    }
    

相关问题