EntityFramework Core问题 - 尝试为Identity表添加迁移 - SQLite

5

我创建了一个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>();
            });
        }
    }
}

非常感谢您的建议和指导。

2个回答

6

您需要安装与您正在使用的EntityFrameworkCore版本相匹配的SQLLite版本;例如,如果都是5.0 preview,则需要相应的版本。


在我的情况下,EFcore包(efcore design,efcore tools,efcore)的版本不同。将它们对齐就可以了! - Amogh Sarpotdar

0

我刚遇到了同样的异常。

检查一下 Nuget 中 Microsoft.EntityFrameworkCore.Tools 的版本,可能是误装了预览版 -5.0。

重新安装 3.1.5 版本,问题就解决了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接