【问题标题】:There is no implicit reference conversion from ApplicationUser to Microsoft.AspNet.Identity.EntityFramework.IdentityUser没有从 ApplicationUser 到 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的隐式引用转换
【发布时间】:2020-09-04 05:19:10
【问题描述】:

我正在尝试试验并在 UserStore 上传递一个自定义 IdentityUser 的通用参数。 所以我继承了 IdentityUser 并使用它所需的通用参数实现了所有必需的接口。

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;

class Program
{
    static void Main(string[] args)
    {
        var userStore = new UserStore<AppUser>(); //This is line that's triggering the error. AppUser has inherited all IdentityUser requirements, but I am not sure what I am missing here.
    }
}

AppUser 有以下实现:

public class AppUser : IdentityUser<int, AppLogin, AppRole, AppClaim>
{

}

public class AppLogin : IdentityUserLogin<int>
{

}

public class AppRole : IdentityUserRole<int>
{

}

public class AppClaim : IdentityUserClaim<int>
{

}

但问题是,由于这个错误,它没有成功构建:

错误 CS0311 类型“CustomIdentity.AppUser”不能用作泛型类型或方法“UserStore”中的类型参数“TUser”。没有从“CustomIdentity.AppUser”到“M​​icrosoft.AspNet.Identity.EntityFramework.IdentityUser”的隐式引用转换。

我已经检查了这篇文章:There is no implicit reference conversion from ApplicationUser to IdentityUser,但它的 IdentityUser 不是我在代码上所做的自定义。

【问题讨论】:

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


    【解决方案1】:

    这是我的答案,我只是错过了定义,而对 Intellisense 的敏锐洞察力适用于这种情况:

    var userStore = new UserStore<AppUser, AppRole, int, AppLogin, AppUserRole, AppClaim>(new MyDbContext(""));
    

    下面是类的定义:

    public class AppUser : IdentityUser<int, AppLogin, AppUserRole, AppClaim>
    {}
    
    public class AppLogin : IdentityUserLogin<int>
    {}
    
    public class AppUserRole : IdentityUserRole<int>
    {}
    
    public class AppClaim : IdentityUserClaim<int>
    {}
    
    public class AppRole : IdentityRole<int, AppUserRole>
    {}
    

    这只是一个过度简化的解释和代码,但它会编译,您将了解如何自定义它。但以一种简单的方式,您可以在几行上完成此操作,因为下面的代码是第一个覆盖定义。

    var userStoreSimplified = new UserStore<AppUserSimplified>();
    

    类定义:

    public class AppUserSimplified : IdentityUser
    {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-25
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多