[英]Issue with EntityFramework Core - trying to add migration for Identity Tables - SQLite
I have created a Razor Pages web application which has its own context class, and works fine with EF Core.Sqlite我创建了一个 Razor Pages web 应用程序,它有自己的上下文 class,并且可以与 EF Core.Z283F04AC146A4012BFABB66 一起正常工作
I decided to add Identity to my application, and currently running into issues when trying to add a migration.我决定将 Identity 添加到我的应用程序中,但目前在尝试添加迁移时遇到了问题。 I've googled the error but no joy.我用谷歌搜索了错误,但并不高兴。
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
is the Context class created by Identity. BakerAppUsersContext
是 Identity 创建的 Context class。 I have registered this Context class within my Startup.cs
:我已经在我的Startup.cs
中注册了这个上下文 class :
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddDbContext<BakeryContext>();
services.AddEntityFrameworkSqlite().AddDbContext<BakeryAppUsersContext>();
}
Below is the Context class added by Identity:下面是 Identity 添加的 Context class:
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);
}
}
}
Finally, below is the code from IdentityHostingStartup.cs
which was created by the scaffolded code.最后,下面是由脚手架代码创建的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>();
});
}
}
}
Any ideas or pointers will be appreciated.任何想法或指针将不胜感激。
You need to install the SQLLite version that matches the EntityFrameworkCore version you are using;您需要安装与您正在使用的 EntityFrameworkCore 版本相匹配的 SQLLite 版本; ie. IE。 if 5.0 preview for both.如果两者都是 5.0 预览版。
I just met the same Exception.我刚刚遇到了同样的异常。
Check the nuget Microsoft.EntityFrameworkCore.Tools version.检查 nuget Microsoft.EntityFrameworkCore.Tools 版本。 It istalled previews version -5.0 by mistake.它错误地安装了预览版-5.0。
reinstall 3.1.5 version.重新安装 3.1.5 版本。 now it's solved现在解决了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.