C# 如何在.Net Identity ApplicationUser模型中添加一列以指向外部模型?

C# 如何在.Net Identity ApplicationUser模型中添加一列以指向外部模型?,c#,asp.net-mvc,entity-framework,asp.net-identity,identity,C#,Asp.net Mvc,Entity Framework,Asp.net Identity,Identity,我正在尝试将Asp.NETMVC框架与Microsoft提供的现成的内置标识系统一起使用 但是,我需要捕获更多关于用户的数据 为了最大限度地减少对标识类的更改,我需要创建一个名为“User”的新模型,在该模型中输入所有自定义属性。然后我添加了一个名为“SystemId”的属性,它是ApplicationUser模型的外键 这就是我所做的 这是我的自定义用户模型 [Required] [MaxLength(128)] [ForeignKey("Id")] publi

我正在尝试将Asp.NETMVC框架与Microsoft提供的现成的内置标识系统一起使用

但是,我需要捕获更多关于用户的数据

为了最大限度地减少对标识类的更改,我需要创建一个名为“User”的新模型,在该模型中输入所有自定义属性。然后我添加了一个名为“SystemId”的属性,它是
ApplicationUser
模型的外键

这就是我所做的

这是我的自定义
用户
模型

    [Required]
    [MaxLength(128)]
    [ForeignKey("Id")]
    public string SystemId { get; set; }


    [MaxLength(50)]
    public string FirstName { get; set; }

    [MaxLength(50)]
    public string MiddleName { get; set; }

    [MaxLength(50)]
    public string LastName { get; set; }

    [MaxLength(250)]
    public string Email { get; set; }

    [MaxLength(50)]
    [Index("IX_Username", 1, IsUnique = true)]
    public string Username { get; set; }

    [ForeignKey("Role")]
    public int RoleId { get; set; }


    [ForeignKey("Client")]
    public int CurrentClientId { get; set; }

    [ForeignKey("Campaign")]
    public int? CurrentCampaignId { get; set; }

    public string Status { get; set; }

    public virtual Role Role { get; set; }

    public virtual Client Client { get; set; }

    public virtual Campaign Campaign { get; set; }

    public virtual ApplicationUser ApplicationUser { get; set; }

    public virtual List<UserToPrivilege> UserToPrivileges { get; set; } 

    public User()
    {
        DateTime UtcNow = DateTime.UtcNow;

        Status = "active";

        MiddleName = null;

        UserToPrivileges = new List<UserToPrivilege>();

        CreatedAt = UtcNow;

        LastUpdatedAt = UtcNow;
    }

}
但是当我尝试这样创建迁移时

Add-Migration InitialCreate -Force
我得到以下错误

无法将属性“Id”配置为导航属性。这个 属性必须是有效的实体类型,并且该属性应具有 非抽象的getter和setter。对于集合属性,类型为 必须实现ICollection,其中T是有效的实体类型


我做错了什么?如何将自定义
用户
模型链接到属于关系中的
应用程序用户
模型?

首先更改类名。然后尝试将
公共虚拟应用程序用户
添加到您的新类中,希望这能奏效,因为这对我有用。 我使用了新类
UserProfile
它看起来像这样:

[Table("UserProfiles")]
public class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public virtual ApplicationUser User { get; set; }
    public string UserId { get; set; }
    public DateTime CreatedAt { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Gender { get; set; }

    public string UrlSeo { get; set; }
}

对我来说很好。

首先更改类名。然后尝试将
公共虚拟应用程序用户
添加到您的新类中,希望这能奏效,因为这对我有用。 我使用了新类
UserProfile
它看起来像这样:

[Table("UserProfiles")]
public class UserProfile
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public virtual ApplicationUser User { get; set; }
    public string UserId { get; set; }
    public DateTime CreatedAt { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Gender { get; set; }

    public string UrlSeo { get; set; }
}

对我来说很好。

我应该更改什么类名?您使用的是类名User,所以如果Identity system生成相同的名称,请尝试更改它,它会工作,我也解决了这个问题,将类名更改为UserProfile或其他,现在可以工作了。我应该更改什么类名?您使用的是类名User,所以,如果Identity system生成相同的名称,那么尝试更改它,它就会工作,我也解决了这个问题,将类名更改为UserProfile或其他,它现在可以工作了。为什么不在User类中继承ApplicationUser?这听起来是个好主意。当我这样做的时候,我会有一个或两个模型吗?从数据视图来看,我会有一个表而不是两个表吗?应该只有一个表,但我不记得我已经有几年没有这样做了。你为什么不在用户类中继承ApplicationUser?这听起来是个好主意。当我这样做的时候,我会有一个或两个模型吗?从数据的角度来看,我会有一个表而不是两个表吗?应该只有一个,但我不记得我已经有几年没有这样做了。