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

tot*_*123 5 c# sqlite entity-framework-core razor-pages asp.net-core-3.1

我创建了一个 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
Run Code Online (Sandbox Code Playgroud)

BakerAppUsersContext是 Identity 创建的 Context 类。我已经在我的 Context 中注册了这个 Context 类Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddDbContext<BakeryContext>();
    services.AddEntityFrameworkSqlite().AddDbContext<BakeryAppUsersContext>();
}
Run Code Online (Sandbox Code Playgroud)

下面是 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,下面是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>();
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何想法或指示将不胜感激。

use*_*525 6

您需要安装与您正在使用的EntityFrameworkCore版本匹配的SQLLite版本;IE。如果两者都可以预览 5.0。


小智 0

我刚刚遇到了同样的异常。

检查 nuget Microsoft.EntityFrameworkCore.Tools 版本 。它错误地安装了预览版本-5.0。

重新安装3.1.5版本。现在已经解决了