gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:06:35 27 4
gpt4 key购买 nike

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

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

最佳答案

您需要安装与您使用的 EntityFrameworkCore 版本匹配的 SQLite 版本; IE。如果两者都有 5.0 预览版。

关于c# - EntityFramework Core 问题 - 尝试为身份表添加迁移 - SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62300927/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com