簡體   English   中英

沒有從 ApplicationUser 到 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的隱式引用轉換

[英]There is no implicit reference conversion from ApplicationUser to Microsoft.AspNet.Identity.EntityFramework.IdentityUser

我正在嘗試在 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”到“Microsoft.AspNet.Identity.EntityFramework.IdentityUser”的隱式引用轉換。

我檢查了這篇文章: 沒有從 ApplicationUser 到 IdentityUser 的隱式引用轉換,但它的 IdentityUser 不是我在代碼中所做的自定義。

這是我的答案,我只是錯過了定義,並且對 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>();

Class定義:

public class AppUserSimplified : IdentityUser
{}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM