![](/img/trans.png)
[英]Unable to create an object of type 'AppDbContext' For the different patterns supported at design time
[英]Unable to create an object of type 'AppDbContext'
為了學習,我正在嘗試進行社交登錄。
我使用例如https://tecknoworks.com/how-to-integrate-social-login-in-a-web-api-solution/按照這個例子我在做 Add-Migration InitialCreate 時無法創建遷移我得到錯誤
“無法創建‘AppDbContext’類型的對象。有關設計時支持的不同模式,請參閱https://go.microsoft.com/fwlink/?linkid=851728”
在搜索找到的使用 add-migration initial -verbose 的建議時,我收到錯誤
“在訪問 Microsoft.Extensions.Hosting 服務時遇到意外的返回類型。方法‘CreateHostBuilder (string [])’應該返回一個類型為‘IHostBuilder’的對象。在沒有應用程序服務提供者的情況下繼續。”
public class Program
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args);
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<Program>();
try
{
var environment = services.GetRequiredService<IWebHostEnvironment>();
if (environment.IsDevelopment())
{
var context = services.GetRequiredService<Infrastructure.AppDbContext>();
MigrateDatabaseToLatestVersion.ExecuteAsync(context).Wait();
}
}
catch (AppException ex)
{
logger.LogError(ex, "An error occurred creating/updating the DB.");
}
}
host.Run();
}
private static IWebHost CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
代碼 AppDbContext
public class AppDbContext : IdentityDbContext<AppUser>
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<AppUser>(entity => { entity.ToTable(name: "tr_Users"); });
builder.Entity<IdentityRole>(entity => { entity.ToTable(name: "tr_Roles"); });
builder.Entity<IdentityUserRole<string>>(entity => { entity.ToTable("tr_UserRoles"); });
builder.Entity<IdentityUserClaim<string>>(entity => { entity.ToTable("tr_UserClaims"); });
builder.Entity<IdentityUserLogin<string>>(entity => { entity.ToTable("tr_UserLogins"); });
builder.Entity<IdentityUserToken<string>>(entity => { entity.ToTable("tr_UserTokens"); });
builder.Entity<IdentityRoleClaim<string>>(entity => { entity.ToTable("tr_RoleClaims"); });
builder.ApplyConfiguration(new AppUserConfiguration());
}
}
配置服務
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
ConfigureSwagger(services);
services.AddDbContext<AppDbContext>(it =>
{
it.UseSqlServer(Configuration["Database:ConnectionString"]);
},
ServiceLifetime.Transient
);
CreateIdentityIfNotCreated(services);
ConfigureAuthenticationSettings(services);
services.AddScoped<IUserService, UserService>();
services.AddMvc(options => options.RespectBrowserAcceptHeader = true);
}
在數據庫表 [__EFMigrationsHistory] 中創建
您需要更改IWebHost CreateWebHostBuilder
方法以返回IHostBuilder
(並相應地返回Main
),在您的情況下,它看起來像這樣:
public static IHostBuilder CreateWebHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
.....
host.Run();
}
此外,我已將CreateWebHostBuilder
更改為使用Host
而不是WebHost
如“Migrate from ASP.NET Core 2.2 to 3.0” docs 中所述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.