在ApplicationDBContext中,点净核心身份用户引用为外部

时间:2018-08-23 07:04:09

标签: .net-core entity-framework-core identity asp.net-core-identity

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

  

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表名并更新数据库,然后当我在UserVehicles表上查询时得到“ ApplicationUser表不存在”

1 个答案:

答案 0 :(得分: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; }
}