Foreign key contraint is incorrectly formed


In my .NET core 3.0 application, i'm trying to add a field with a constraint to my overridden ApplicationUser entity (overridden from IdentityUser). My database is a MySql database.

My models look like this:

using Microsoft.AspNetCore.Identity;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Cnock.Models
    public class ApplicationUser : IdentityUser
        public DateTime CreatedDateTime { get; set; }

        public string FirstName { get; set; }

        public string Name { get; set; }

    public class CatalogProductBid
        public int Id { get; set; }

        public int CatalogProductId { get; set; }

        public virtual CatalogProduct CatalogProduct { get; set; }

        public DateTime CreatedDateTime { get; set; }

        // Start added code
        public string UserId { get; set; }

        public virtual ApplicationUser ApplicationUser { get; set; }

My database context looks like this:

public class ApplicationDbContext : IdentityDbContext
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)

        public DbSet<Category> Categories { get; set; }

        public DbSet<ProductImage> ProductImages { get; set; }

        public DbSet<Product> Products { get; set; }

        public DbSet<Country> Countries { get; set; }

        public DbSet<Catalog> Catalogs { get; set; }

        public DbSet<CatalogProduct> CatalogProducts { get; set; }

        public DbSet<ApplicationUser> ApplicationUsers { get; set; }

        public DbSet<ContactForm> ContactForms { get; set; }

        public DbSet<UserCatalog> UserCatalogs { get; set; }

        public DbSet<UserCatalogProduct> UserCatalogProducts { get; set; }

        public DbSet<Origin> Origins { get; set; }

        public DbSet<CatalogProductBid> CatalogProductBids { get; set; }

Following code is generated as a migration:

protected override void Up(MigrationBuilder migrationBuilder)
                name: "UserId",
                table: "CatalogProductBids",
                nullable: false,
                defaultValue: "");

                name: "IX_CatalogProductBids_UserId",
                table: "CatalogProductBids",
                column: "UserId");

                name: "FK_CatalogProductBids_AspNetUsers_UserId",
                table: "CatalogProductBids",
                column: "UserId",
                principalTable: "AspNetUsers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

When executing the migration i get the error:

CommandType='Text', CommandTimeout='30'] ALTER TABLE
`CatalogProductBids` ADD CONSTRAINT
`FK_CatalogProductBids_AspNetUsers_UserId` FOREIGN KEY (`UserId`)
MySql.Data.MySqlClient.MySqlException (0x80004005): Can't create table
`cnockbe`.`#sql-1e35_7f3` (errno: 150 "Foreign key constraint is
incorrectly formed")  ---> MySql.Data.MySqlClient.MySqlException
(0x80004005): Can't create table `cnockbe`.`#sql-1e35_7f3` (errno: 150
"Foreign key constraint is incorrectly formed") ```

Any idea what might be wrong with this? Thank you!

asked on Stack Overflow Jan 19, 2020 by Roel • edited Jan 20, 2020 by Roel

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0