我创建了一个Razor Pages Web应用程序,它有自己的上下文类,并且在使用EF Core.Sqlite时运行良好。
我决定向我的应用程序添加Identity,但在尝试添加迁移时遇到问题。我已经谷歌搜索了错误,但没有解决。
PM> Add-Migration BakeryIdentity -Context BakeryAppUsersContext
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Method 'Create' in type 'Microsoft.EntityFrameworkCore.Sqlite.Query.Internal.SqliteQueryableMethodTranslatingExpressionVisitorFactory' from assembly 'Microsoft.EntityFrameworkCore.Sqlite, Version=3.1.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.
Unable to create an object of type 'BakeryAppUsersContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
BakerAppUsersContext
是 Identity 创建的 Context 类。我在 Startup.cs
中注册了这个 Context 类:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddDbContext<BakeryContext>();
services.AddEntityFrameworkSqlite().AddDbContext<BakeryAppUsersContext>();
}
以下是Identity添加的Context类:
using BakeryApp.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace BakeryApp.Data
{
public class BakeryAppUsersContext : IdentityDbContext<BakeryAppAdmin>
{
public BakeryAppUsersContext(DbContextOptions<BakeryAppUsersContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(@"Data source=Bakery.db");
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
}
最后,以下是由生成的代码创建的IdentityHostingStartup.cs
代码。
using BakeryApp.Areas.Identity.Data;
using BakeryApp.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
[assembly: HostingStartup(typeof(BakeryApp.Areas.Identity.IdentityHostingStartup))]
namespace BakeryApp.Areas.Identity
{
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<BakeryAppUsersContext>(options =>
options.UseSqlite(
context.Configuration.GetConnectionString("BakeryAppUsersContextConnection")));
services.AddDefaultIdentity<BakeryAppAdmin>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<BakeryAppUsersContext>();
});
}
}
}
非常感谢您的建议和指导。