【问题标题】:Inherit a Class from 2 base classes one is my class other is AspNet IdentityUser从 2 个基类继承一个类,一个是我的类,另一个是 AspNet IdentityUser
【发布时间】:2020-11-16 02:08:02
【问题描述】:

我有一个Person 实体类,它继承自我的BaseEntity。项目结构位于该基础实体类上,因此我无法更改或删除它。

我想实现另一个来自Microsoft.AspNetCore.Identity的基类IdentityUser

但出现错误:“Person”类不能有多个基类:“BaseEntity”和“IdentityUser”

有没有办法解决这个问题?

public class Person : BaseEntity, IdentityUser
{
    public int GroupId { get; set; }
    public string CardNumber { get; set; }
    public int PrivilegeId { get; set; }
    public string Name { get; set; }
    public string MiddleName { get; set; }
    public string Surname { get; set; }
    public string Password { get; set; }
    ........
    ........
    ........
}

【问题讨论】:

    标签: c# entity-framework-core asp.net-identity


    【解决方案1】:

    C# 不像 C++。不能继承比基类更多的形式。也许您应该将 IdentityUser 转换为接口。

    public interface IIdentityUser
    {
      int GroupId { get; set; }
      int PrivilegeId { get; set; }
      // ...
    }
    
    public class Person : BaseEntity, IIdentityUser
    {
      public int GroupId { get; set; }
      public string CardNumber { get; set; }
      public int PrivilegeId { get; set; }
      // ...
    }
    

    在多继承系统中可能会出现菱形问题(请参阅Wikipedia)。还有一个讨论here

    【讨论】:

    • 谢谢你,我也会看看那个讨论
    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2014-11-22
    • 1970-01-01
    • 2014-09-25
    • 2012-04-29
    • 2021-09-03
    相关资源
    最近更新 更多