Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with EntityFramework Core - trying to add migration for Identity Tables - SQLite

Tags:

I have created a Razor Pages web application which has its own context class, and works fine with EF Core.Sqlite

I decided to add Identity to my application, and currently running into issues when trying to add a migration. 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. I have registered this Context class within my Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddDbContext<BakeryContext>();
    services.AddEntityFrameworkSqlite().AddDbContext<BakeryAppUsersContext>();
}

Below is the Context class added by Identity:

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.

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.

like image 923
totestornot123 Avatar asked Jun 10 '20 10:06

totestornot123


1 Answers

You need to install the SQLLite version that matches the EntityFrameworkCore version you are using; ie. if 5.0 preview for both.

like image 115
user1012525 Avatar answered Sep 30 '22 21:09

user1012525