首页 文章

EF Core 2.0.1使用IdentityUser作为导航属性

提问于
浏览
2

当我尝试注册用户或创建迁移时,我收到以下错误:

“无法在'ApplicationUser'上配置密钥,因为它是派生类型 . 必须在根类型'IdentityUser'上配置密钥 . 如果您不打算将'IdentityUser'包含在模型中,请确保它不包含在上下文中的DbSet属性中,在对模型构建器的配置调用中引用,或者从模型中包含的类型的导航属性引用 .

我有一个BaseEntity,一切都来自于:

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

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

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

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

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

        public virtual IdentityUser CreatedBy { get; set; }
        public virtual IdentityUser ModifiedBy { get; set; }
    }

public class FilePath : BaseEntity, IAuditable
    {
        [StringLength(255)]
        public string FileName { get; set; }
        public FileType FileType { get; set; }
    }

是否有新规则或更新表明您不能将IdentityUser用作导航属性?谷歌没有带来太多有用的信息 .

如果需要,整个解决方案是here .

更新:升级到2.0.1预览后,错误会更有帮助:

外键属性{'Id':string}的最佳匹配与主键{'Id':int}不兼容 .

1 回答

  • 0

    回覆 . “外键属性的最佳匹配{'Id':string} ......”

    您的 ApplicationUser 类未指定其Id字段的类型 . 在这种情况下,它默认为 string . 你可以做什么 - 虽然这会导致你重建你的数据库,所以现在可能太晚了 - 是明确键入 ApplicationUser

    public class ApplicationUser: IdentityUser<int>
    {
        // myUser.Id is now an int
    }
    

    IdentityUser docs

相关问题