Foreign key contraint is incorrectly formed

0

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
    {
        [Required]
        public DateTime CreatedDateTime { get; set; }

        [Required]
        public string FirstName { get; set; }

        [Required]
        public string Name { get; set; }
    }

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

        [Required]
        public int CatalogProductId { get; set; }

        [ForeignKey("CatalogProductId")]
        public virtual CatalogProduct CatalogProduct { get; set; }

        [Required]
        public DateTime CreatedDateTime { get; set; }

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

        [ForeignKey("UserId")]
        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)
        {
            migrationBuilder.AddColumn<string>(
                name: "UserId",
                table: "CatalogProductBids",
                nullable: false,
                defaultValue: "");

            migrationBuilder.CreateIndex(
                name: "IX_CatalogProductBids_UserId",
                table: "CatalogProductBids",
                column: "UserId");

            migrationBuilder.AddForeignKey(
                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`)
REFERENCES `AspNetUsers` (`Id`) ON DELETE CASCADE;
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!

mysql
entity-framework
asp.net-core
.net-core
entity-framework-core
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