Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identity asp.net core 3.0 - IdentityDbContext not found

My app broke with the 3.0 release of .NET core with reference errors for IdentityDbContext. I'm looking through documentation for Identity on core 3.0 but it implies that IdentityDbContext should be there. It's the only error I'm getting with a couple DbContext errors.

I have a pretty simple API, no MVC views, just a data server that gives back JSON objects. It's based on Identity so it has the users and roles and claims. And it's starting to take advantage of that. My main DbContext extends IdentityDbContext<ApplicationUser> but after switching target platform to 3.0 after the upgrade, it says it doesn't exist and gives me compile errors. Has anyone run into this? Am I missing something? The migration and breaking changes pages don't seem to have anything addressing my issue.

DbContext looks like this:

using System;
using Microsoft.AspNetCore.Identity;
//using Microsoft.AspNetCore.Identity.EntityFrameworkCore; <- this no longer works either
using Microsoft.EntityFrameworkCore; //<- this I had to download as a package
using App.Constants;
using App.Models.Identity;

namespace App.Models
{
    public class AppContext : IdentityDbContext<ApplicationUser> //<- error is right here
    {
        ... my models
    }
}
like image 988
Paul Carlton Avatar asked Sep 25 '19 01:09

Paul Carlton


2 Answers

In ASP.NET Core 3.0, Entity Framework Core and Identity related packages have been removed from the Microsoft.AspNetCore.App metapackage. So you have to add those packages separately.

Add the following PackageReferences to your project's .csproj file as follows:

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0" />
</ItemGroup>

Now it will work!

For more details: Assemblies removed from the ASP.NET Core shared framework

like image 117
TanvirArjel Avatar answered Nov 01 '22 11:11

TanvirArjel


if you have an error like "error CS0246: The type or namespace name 'IdentityDbContext' could not be found" you can install bellow package for asp.net web api 3 project

dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore --version 5.0.0-preview.3.20215.14 in dotnet cli.its work for me.

Install-Package Microsoft.AspNetCore.Identity.EntityFrameworkCore -Version 5.0.0-preview.3.20215.14 this for package manager

like image 28
pamal Sahan Avatar answered Nov 01 '22 12:11

pamal Sahan