【发布时间】:2021-03-14 15:42:53
【问题描述】:
当我跑步时
dotnet ef migrations add IdentityInitial -p Data -s SignalRreactjs -c IdentityAppDbContext -o Identity/Migrations
我收到了回复
访问 Microsoft.Extensions.Hosting 时出错 服务。在没有应用程序服务提供商的情况下继续。错误: 某些服务无法构建(验证时出错 服务描述符'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator 生命周期:范围 实施类型:Microsoft.AspNetCore.Identity.Secu rityStampValidator
1[Data.Models.AppUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Authentication.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Ide ntity.SecurityStampValidator1[Data.Models.AppUser]'。)(出错时 验证服务描述符 'ServiceType: Microsoft.AspNetCore.Identity.IwoFactorSecurityStampValidator Lifet ime:范围实施类型: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator1[Data.Models.AppUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Authenti cation.ISystemClock' while attempting to activate 'Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator1[Data.Models.AppUser]'。) 无法创建“IdentityAppDbContext”类型的对象。为了 设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<IdentityAppDbContext>(x =>
{
x.UseSqlServer(_config.GetConnectionString("IdentityConnection"));
});
services.AddIdentityCore<AppUser>().AddEntityFrameworkStores<IdentityAppDbContext>()
.AddSignInManager<SignInManager<AppUser>>();
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo {Title = "SignalRreactjs", Version = "v1"});
});
}
IdentityAppDbContext.cs
using Data.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Data.Identity
{
public class IdentityAppDbContext : IdentityDbContext<AppUser>
{
public IdentityAppDbContext(DbContextOptions<IdentityAppDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
}
AppUser.cs
using Microsoft.AspNetCore.Identity;
namespace Data.Models
{
public class AppUser : IdentityUser
{
public string DisplayName { get; set; }
public string ImageUrl { get; set; }
}
}
Data.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.4" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
</ItemGroup>
</Project>
SignalRreactjs.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-preview.2.21154.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Data\Data.csproj" />
</ItemGroup>
</Project>
【问题讨论】:
-
嗨@lizardcoder,我想你可以查看this answer。
标签: c# asp.net-core asp.net-web-api