【问题标题】:Dot Net Core Identity User reference as Foreign in ApplicationDBContext在 ApplicationDBContext 中,Dot Net Core Identity 用户引用为 Foreign
【发布时间】:2019-01-29 11:52:35
【问题描述】:

我正在使用 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 表不存在”

【问题讨论】:

  • 您确定迁移是在同一个数据库上执行的吗?也许它需要一些默认连接字符串而不是您期望的。
  • @RuardvanElburg 是的,我正在使用相同的连接字符串,下面来自 ConfigureServices services.AddDbContext(options => options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection"))); services.AddDbContext(options => options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")));
  • @RuardvanElburg 你能帮忙吗
  • 这就是为什么你最好不要混合上下文。在此处阅读我的答案以获得解释:stackoverflow.com/questions/51934680/… 您可以尝试不将 UserVehicle 添加到 Identity 上下文中。

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


【解决方案1】:

我曾经遇到过同样的问题,但另一边也有导航属性。解决这个我使用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; }
}

【讨论】:

  • 我尝试使用 ForiegnKey 表中的 Inverse 属性和 ApplicationUser 表中的 ICollection 属性,但仍然为 ApplicationUser 表创建创建了迁移。 @Roxtar
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 2017-12-16
相关资源
最近更新 更多