![](/img/trans.png)
[英]No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered
[英]Cannot cast 'Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1[Microsoft.AspNetCore.Identity.IdentityUser`1[System.Int32]]'
問題:
System.InvalidCastException:“無法將類型為‘Microsoft.EntityFrameworkCore.Internal.InternalDbSet 1[Microsoft.AspNetCore.Identity.IdentityUser
1[System.Int32]]”的 object 轉換為鍵入“System.Linq.IQueryable”1[DAL.User ]'.'
相關代碼:
...
public class SocialNetworkDbContext : IdentityDbContext<IdentityUser<int>, IdentityRole<int>, int>
...
public IQueryable<User> FindAll()
{
var allUsers = (IQueryable<User>)_socialNetworkDbContext.Users;
if (allUsers == null)
{
throw new NoRecordFoundException();
}
return allUsers;
試過:
public class SocialNetworkDbContext: IdentityDbContext<User>, IdentityRole<int>, int>
//沒有例外,但這段代碼中斷
var result = await _userManager.CreateAsync(user, model.Password);
await _userManager.AddToRoleAsync(user, model.Role);
//同樣的異常
3)
public IQueryable<User> FindAll()
{
var allUsers = (IQueryable<User>)(IEnumerable<User>)_socialNetworkDbContext.Users;
if (allUsers == null)
{
throw new NoRecordFoundException();
}
return allUsers;
// 相同的異常(無法轉換為 IEnumerable)。 投射到 ICollection(無法投射到 ICollection)
非常感謝任何建議!
您應該更改 DbContext 以使用實際的用戶類型:
public class SocialNetworkDbContext : IdentityDbContext<User, IdentityRole<int>, int>
{
}
如果角色有 class,則也應將其用作通用類型參數。 這樣你就不需要任何類型轉換。
從您的代碼看來, _socialNetworkDbContext.Users
存儲的是 IdentityUser,而不是用戶 model,對嗎? 所以,從Users表中查詢數據后,可以使用如下代碼對查詢結果進行轉換:
public IQueryable<User> FindAll()
{
var allUsers = _socialNetworkDbContext.Users.Select(c => new User()
{
UserName = c.UserName,
Email = c.Email,
//... other properties
}).AsQueryable();
if (allUsers == null)
{
throw new NoRecordFoundException();
}
return allUsers;
}
更新
您可以參考以下示例代碼:
添加一個ApplicationUser class 自定義Identity User model:
public class ApplicationUser : IdentityUser<int>
{
public string CustomTag { get; set; }
}
使用 ApplicationUser 類型作為上下文的通用參數:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>
{
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
更新 Pages/Shared/_LoginPartial.cshtml 並將 IdentityUser 替換為 ApplicationUser:
@using Microsoft.AspNetCore.Identity
@using WebApp1.Areas.Identity.Data
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
更新Startup.ConfigureServices
並將IdentityUser
替換為ApplicationUser
:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddIdentity<ApplicationUser, IdentityRole<int>>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
services.AddControllersWithViews();
}
然后,啟用遷移並生成數據庫。
創建新用戶后,AspNetUser 表數據如下:
創建一個 UserViewModel 並添加所需的屬性:
public class UserViewModel
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Tag { get; set; }
}
然后,在Controller中,使用如下代碼查詢user表:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly ApplicationDbContext _context;
public HomeController(ILogger<HomeController> logger, ApplicationDbContext context)
{
_logger = logger;
_context = context;
}
public IActionResult Index()
{
var result = FindAll();
return View();
}
public IQueryable<UserViewModel> FindAll()
{
var allUsers = _context.Users.Select(c => new UserViewModel()
{
UserId = c.Id,
UserName = c.UserName,
Tag = c.CustomTag
});
//if (allUsers == null)
//{
// throw new NoRecordFoundException();
//}
return allUsers;
}
結果如下:
先試試(); 或 FirstOrDefault();
改變你的代碼:
public async Task<List<User>> GetAllUsers()
{
var allUsers = await _socialNetworkDbContext.Users.ToListAsync();
if (allUsers == null)
{
throw new NoRecordFoundException();
}
return allUsers;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.