Dot Net Core Identity User reference as Foreign in ApplicationDBContext

.net-core asp.net-core-identity entity-framework-core identity

Question

I am using the Identity DB context and the Application DB context, I have a table (UserVehicles) in which I did foreign key reference with the ApplicationUser(Identity User) property, when I add migration for the ApplicationDBContext it is creating the script for new table "ApplicationUser" and adding foreign key relation to the new table instead of the default AspnetUsers(Identity User) table. Below are the classes

IdentityDBContext class

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

ApplicationDBContext class

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

below is the migration that is created

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

I tried to remove the code for ApplicationUser in migration class also update the table name to Identity Users Table name and update the database then when I do query on the UserVehicles table I am getting "ApplicationUser table does not exist"

1
0
8/28/2018 4:15:10 AM

Popular Answer

I had same issue once but had also navigation properties on the other side. The resolve this I used InverseProperty attribute.

Maybe this will work.

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; }
}
0
8/23/2018 8:38:16 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow