我正在嘗試使用 ASP.NET Core Web API .NET 6.0 為 Entity Framework Core 搭建控制器。一切都建立起來了,我可以添加遷移和更新資料庫。當我嘗試腳手架控制器時,我收到此錯誤..

這是我的啟動
using System;
using System.Net;
using System.Text;
using AutoMapper;
using Kasica.API.Business.Interfaces;
using Kasica.API.Business.Interfaces.External;
using Kasica.API.Business.Interfaces.Repositories;
using Kasica.API.Business.Services;
using Kasica.API.Business.Services.External;
using Kasica.API.Common.Entities;
using Kasica.API.Data;
using Kasica.API.Data.Extensions;
using Kasica.API.Data.Repositories;
using Kasica.API.Filters;
using Kasica.API.Helpers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
namespace Kasica.API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//konfiguracija identity
IdentityBuilder builder = services.AddIdentityCore<User>(opt =>
{
opt.Password.RequireDigit = false;
opt.Password.RequiredLength = 6;
opt.Password.RequireNonAlphanumeric = false;
opt.Password.RequireUppercase = false;
});
builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole), builder.Services);
builder.AddEntityFrameworkStores<DataContext>();
builder.AddRoleValidator<RoleValidator<IdentityRole>>();
builder.AddRoleManager<RoleManager<IdentityRole>>();
builder.AddSignInManager<SignInManager<User>>();
builder.AddTokenProvider<DataProtectorTokenProvider<User>>(TokenOptions.DefaultProvider);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
ValidateIssuer = false,
ValidateAudience = false
};
});
services.AddControllers()
.AddNewtonsoftJson(opt =>
{
opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Kasica API",
Description = "A simple example ASP.NET Core Web API",
//TermsOfService = new Uri("https://example.com/terms"),
//Contact = new OpenApiContact
//{
// Name = "Shayne Boyer",
// Email = string.Empty,
// Url = new Uri("https://twitter.com/spboyer"),
//},
//License = new OpenApiLicense
//{
// Name = "Use under LICX",
// Url = new Uri("https://example.com/license"),
//}
});
});
//extension u data layeru kako ne bi trebali imati entity framework referenciran od API layera
services.AddDataAccessServices(Configuration.GetConnectionString("KasicaConnection"));
services.AddCors();
//services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //radi i ovo samo ovako
services.AddAutoMapper(cfg => cfg.AddProfile<AutoMaperProfiles>(),
AppDomain.CurrentDomain.GetAssemblies());
services.AddScoped<IAuthRepository, AuthRepository>();
services.AddScoped<IAuthService, AuthService>();
services.AddScoped<IErrorRepository, ErrorRepository>();
services.AddScoped<IErrorService, ErrorService>();
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IEmailService, EmailService>();
services.AddScoped<IKlijentRepository, KlijentRepository>();
services.AddScoped<IKlijentService, KlijentService>();
services.AddScoped<LogUserActivity>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
//}
//else
//{
app.UseExceptionHandler(builder => //globalno exception handlanje
{
builder.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var error = context.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
//context.Response.AddApplicationError(error.Error.Message); //ne?u koristiti errore u headeru zbog problema sa encodingom cro znakova
await context.Response.WriteAsync(error.Error.Message);
}
});
});
//}
//app.UseHttpsRedirection();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.)
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
//c.RoutePrefix = string.Empty;
});
app.UseRouting();
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
我只有一個對 Microsoft.AspNetCore.Identity 的參考

我嘗試 unistall 并安裝所有軟體包,但沒有任何效果,我不知道接下來要嘗試什么。
編輯
我在參考的專案“資料”層中使用 SignInManager 并暫時洗掉并使用 UserManager 來檢查密碼......(我看到啟動時的 SignInManager 類正在使用命名空間“Microsoft.AspNetCore.Identity”,但我不知道來自哪個包,并且在資料專案中,如果我想使用它,我需要安裝 Microsoft.AspNetCore.Identity 包 2.2.0 !!!!!!?) 沒有參考“Microsoft.AspNetCore.Identity”包 2.2.0 現在錯誤是不同的但是仍然無法修復它。

uj5u.com熱心網友回復:
洗掉 Microsoft.AspnetCore.Identity 包,不再需要它。
https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-6.0&tabs=visual-studio
uj5u.com熱心網友回復:
根據這篇文章https://docs.microsoft.com/hr-hr/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli 在設計時創建 DataContext 時出現問題。我添加了這個類,現在腳手架正在作業。
public class DataContextFactory : IDesignTimeDbContextFactory<DataContext>
{
public DataContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
var optionsBuilder = new DbContextOptionsBuilder<DataContext>();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("KasicaConnection"));
return new DataContext(optionsBuilder.Options);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/401923.html
標籤:C# 网站 asp.net核心 asp.net-core-脚手架
下一篇:Automapper映射例外
