Reputation: 131
The purpose of providing a .NET Standard 2.1 version of EF6 is understood to be to assist the upgrading of existing projects in stages.
So we have an existing solution that consists of websites, a windows service and console apps. We are in the process of upgrading it from .NET Framework to .NET Core.
It would be preferable to get it all working again in .NET Core but while still using the .NET Standard 2.1 version of EF6 for now.
The EF6 model derives from IdentityDbContext
so as to contain the identity store of users, roles etc.
public class DbContextTasking : IdentityDbContext<AspNetUser, AspNetRole, string, AspNetUserLogin, AspNetUserRole, AspNetUserClaim>
All the code is now in either .NET Core or .NET Standard assemblies.
The upgraded windows service and console apps essentially work, however we are struggling with the websites.
Although of concern is that in order for the datalayer assembly to compile a number of packageReferences are necessary that show yellow triangle warnings in solution explorer :
<PackageReference Include="Microsoft.AspNet.Identity.EntityFramework" Version="2.2.3" />
"Package x was restored using .NET Framework 4.6.1 .... This package may not be fully compatible ..."
Ignoring these warnings for now, the show stopper is understanding how to get a ASP.NET Core website with identity, authentication and authorization wired up to use EF6.
For example, examples showing EF Core usage have in their ConfigureServices
function :
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
However '.UseSqlServer' comes from the assembly Microsoft.EntityFrameworkCore.SqlServer
And also with :
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<DbContextTasking>()
.AddDefaultTokenProviders();
AddEntityFrameworkStores
again relates to an EF Core store.
E.g. 2
This package forms the bridge between ASP.NET Core and the identity infrastructure :
Microsoft.AspNetCore.Identity.EntityFrameworkCore
So one would think that to have EF6 working with ASP.NET Core there would need to be a package similar in nature (made up name) of Microsoft.AspNetCore.Identity.EntityFramework6
If it is possible to use EF6.4 (inc. identity related tables) in conjunction with ASP.NET Core, please can you point to an example that achieve this?
Or if you know for a fact that this combination is definitely not possible please also let me know.
Upvotes: 2
Views: 329
Reputation: 239290
EF6 is not supported for ASP.NET Core Identity; you can only use EF Core. Microsoft likes to casually suggest that you can always just use EF6 whenever there's some deficiency in EF Core (and there are tons, constantly), yet there's still this glaring support vacuum since the birth of .NET Core.
You have three options:
Use EF Core for the places where you need ASP.NET Core Identity support. You'll have to duplicate some code, but you can have an EF Core context and an EF 6 context that both connect to the same database. You should just treat one end or the other (most likely the EF Core side) as database-first, and therefore not run migrations from there. However, since ASP.NET Core Identity is fundamentally different from ASP.NET Identity, you will need to replace/migrate any existing Identity tables to the Core versions, which then shuts down any usage of ASP.NET Identity. In other words, you'll have to choose one or the other, exclusively.
ASP.NET Core Identity can technically work with any data source. EF Core is just what's provided out of the box. If you have the time and inclination, you can implement custom stores using whatever technology you want. It's a meaty task, but achievable if that's what you need.
You can use a centralized auth provider like Identity Server. This essentially abstracts the auth layer, so it doesn't really much matter whether the app is in ASP.NET or ASP.NET Core. However, Identity Server 4 only natively integrates with ASP.NET Core Identity and EF Core (same issue essentially). There's an older version of Identity Server that was built for ASP.NET, but I have no idea about the status of this (whether it is still supported, maintained, etc.) or whether you can make that work with ASP.NET Core. Frankly, if your goal is cross-compat between .NET Framework and .NET Core, I'd go with something like Azure AD or Auth0, which can work with either. They're also plug-and-play for the most part, whereas Identity Server requires a bit more elbow grease.
Upvotes: 2