简体   繁体   English

无法在IdentityUser上设置外键

[英]Foreign key on IdentityUser cannot be set

I'm trying to create a new model with a foreign key to an IdentityUser using .NET Core 2.1 and Entity Framework Core (backed by MySQL) using the code-first approach. 我正在尝试使用代码优先方法使用.NET Core 2.1和Entity Framework Core(由MySQL支持)使用对IdentityUser的外键创建新模型。 I've created a model that looks like this: 我创建了一个看起来像这样的模型:

using System;
using Microsoft.AspNetCore.Identity;

namespace Models
{
    public class Note
    {
        public int NoteId { get; set; }

        public string NoteText { get; set; }

        public string CreatedByUserId { get; set; }
        public virtual IdentityUser CreatedByUser { get; set; }
    }
}

Generating the migration looks good, but when I actually try to run the database update, I get a "Cannot add foreign key contstraint" error. 生成迁移看起来不错,但是当我实际尝试运行数据库更新时,出现“无法添加外键约束”错误。

Here's the SQL statement it's failing on: 这是失败的SQL语句:

ALTER TABLE `Notes` ADD CONSTRAINT `FK_Notes_AspNetUsers_CreatedByUserId` FOREIGN KEY (`CreatedByUserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE NO ACTION;

That results in a slightly more detailed error message: "There is no index in the referenced table where the referenced columns appear as the first columns." 这将导致稍微更详细的错误消息: “在引用表中没有索引,其中引用列显示为第一列。”

However, when I look at the indexes on the AspNetUsers table, I can see that Id is the primary key, which should be indexed. 但是,当我查看AspNetUsers表上的索引时,可以看到Id是主索引,应该对其进行索引。 Here's the create statement for the table: 这是表的create语句:

CREATE TABLE aspnetusers
(
  Id                   VARCHAR(127) NOT NULL
    PRIMARY KEY,
  AccessFailedCount    INT          NOT NULL,
  ConcurrencyStamp     LONGTEXT     NULL,
  Email                VARCHAR(256) NULL,
  EmailConfirmed       BIT          NOT NULL,
  LockoutEnabled       BIT          NOT NULL,
  LockoutEnd           DATETIME(6)  NULL,
  NormalizedEmail      VARCHAR(256) NULL,
  NormalizedUserName   VARCHAR(256) NULL,
  PasswordHash         LONGTEXT     NULL,
  PhoneNumber          LONGTEXT     NULL,
  PhoneNumberConfirmed BIT          NOT NULL,
  SecurityStamp        LONGTEXT     NULL,
  TwoFactorEnabled     BIT          NOT NULL,
  UserName             VARCHAR(256) NULL,
  CONSTRAINT UserNameIndex
  UNIQUE (NormalizedUserName)
)
  ENGINE = InnoDB
  CHARSET = latin1;

CREATE INDEX EmailIndex
  ON aspnetusers (NormalizedEmail);

I have also tried using NormalizedUserName and NormalizedEmail in place of Id since they both appear to be indexed fields that show up as the only field in their respective indexes, but I get the same error message for all of them. 我还尝试使用NormalizedUserNameNormalizedEmail代替Id因为它们似乎都是被索引的字段,在它们各自的索引中显示为唯一的字段,但是我对所有它们都得到相同的错误消息。

How do I go about setting up a foreign key to an IdentityUser table? 我该如何为IdentityUser表设置外键?

It turns out the problem for me was that the two tables that referenced each other weren't using the same character set, so MySQL was refusing to set up the foreign key constraint. 事实证明,对我来说问题是两个互相引用的表没有使用相同的字符集,因此MySQL拒绝设置外键约束。 I think it may be a case where I imported the initial db from a mysqldump, but the defaults on my local db didn't match what was output to the sql dump. 我认为可能是从mysqldump导入初始数据库的情况,但是本地数据库的默认数据库与sql dump的输出不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM