Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net EF Core 2.0.1使用IdentityUser作为导航属性_Asp.net_Asp.net Mvc_Entity Framework_Entity Framework Core - Fatal编程技术网

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

Asp.net EF Core 2.0.1使用IdentityUser作为导航属性,asp.net,asp.net-mvc,entity-framework,entity-framework-core,Asp.net,Asp.net Mvc,Entity Framework,Entity Framework Core,当我尝试注册用户或创建迁移时,会出现以下错误: “无法在'ApplicationUser'上配置密钥,因为它是 派生类型。必须在根类型上配置密钥 “IdentityUser”。如果您不打算使用“IdentityUser” 包含在模型中,请确保它未包含在DbSet中 属性,在对的配置调用中引用 ModelBuilder,或从以下类型的导航属性引用 该模型中包含了。” 我有一个基本实体,所有东西都是这样产生的: public class BaseEntity { public

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

“无法在'ApplicationUser'上配置密钥,因为它是 派生类型。必须在根类型上配置密钥 “IdentityUser”。如果您不打算使用“IdentityUser” 包含在模型中,请确保它未包含在DbSet中 属性,在对的配置调用中引用 ModelBuilder,或从以下类型的导航属性引用 该模型中包含了。”

我有一个基本实体,所有东西都是这样产生的:

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用作导航属性?谷歌没有带来多少有用的信息

整个解决方案是 如果需要的话

更新:升级到2.0.1预览版后,错误会更有用:

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

Re。外键属性{'Id':string}的最佳匹配项

您的
ApplicationUser
类未指定其Id字段的类型。在这种情况下,它默认为
string
。您可以做的是显式地键入
ApplicationUser
,尽管这会导致您重建数据库,所以现在可能太晚了:

public class ApplicationUser: IdentityUser<int>
{
    // myUser.Id is now an int
}
公共类应用程序用户:IdentityUser
{
//Id现在是一个int
}

我也面临同样的问题。您需要替换代码中使用BaseEntity到FilePath的所有位置

例如: 我的遗产是这样的:

public class ApplicationUser : IdentityUser
    {

    }
我更改了ApplicationDbContext,如下所示:

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

        }


        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }
    }
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
}
}
但我也有同样的例外


在代码中将IdentityUser替换为ApplicationUser后,错误消失了。我认为您应该通过数据访问层中的
FilePath
使用
BaseEntity

我认为您指的是2.1预览。相同的错误消息,但我正在使用脚手架从数据库构建模型。我认为
不兼容的
消息将消失,它只是
信息,而不是错误。我想也许你还有一个问题。