【问题标题】:Add custom index to entity that inherits from User, which inherits from IdentityUser in EF将自定义索引添加到继承自 User 的实体,该实体继承自 EF 中的 IdentityUser
【发布时间】:2018-11-08 06:49:31
【问题描述】:

我正在使用用户身份制作 ASP.NET MVC 项目。我的 User 类现在继承自 IdentityUser:

  [Table("Users")]
public class User : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Patronymic { get; set; } // Отчество
    public string Avatar { get; set; }
    public DateTime? DateOfBirth { get; set; } // Дата рождения
    public DateTime? RegisterDate { get; set; } // Actualy it is a dateTime :)
    public bool? Sex { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

然后我创建了 Doctor 类,它继承自它:

  [Table("Doctors")]
public class Doctor : User
{
    [Key]
    public int DoctorId { get; set; }
    public string CurriculumVitae { get; set; }
}

如您所见,我添加了自定义索引字段 DoctorId。 但 UserManager 无法创建实体 所以问题是如何添加 CUSTOM INT ID 索引?

【问题讨论】:

标签: c# sql-server asp.net-mvc entity-framework


【解决方案1】:

我找到了解决方案!要强制实体创建密钥,我们需要添加:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DoctorId { get; set; }

而不是这个

[Key]
public int DoctorId { get; set; }

但这不是最好的解决方案..此外不要从用户继承类到实现角色的类!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多