Error when add model type ViewModel to DbContext

170 Views Asked by At

When I want to scaffold a controller with EF Core Identity I got an error like this

Error

There was an error running the selected code generator: 'Could not add Model type

'Electrical.Models.ViewModels.RegisterViewModel' to

DbContext 'DataAccessLayer.AppDbContext'. Make sure that DataAccessLayer.AppDbContext' has a DbSet property for

"Electrical.Models.ViewModels.RegisterViewModel"."

OK

I understand that if I add factory context class besides my context class the problem solved and I am trying, but it does not work.

1

There are 1 best solutions below

1
On

RegisterViewModel is a view model and does not map directly to a database table. In a database context, you should only include entity models that interact with the database, not view models. View models are typically used on the front end of the application to interact with users and are not stored directly in the database, so you should use entity models instead of view models.

identity dose not have model itself so what should I do ?

Identity is part of the ASP.NET Core framework and is used to handle authentication and authorization functions. It integrates many classes: IdentityUser, IdentityRole, IdentityUserRole, IdentityUserToken, IdentityUserLogin, IdentityUserClaim. In between, IdentityUser is one of the classes defined in the ASP.NET Core Identity framework that represents an application's user entity. It provides a standard set of properties to store basic user information such as username, email address, password hash, etc.So you can create a user entity class, derived from IdentityUser:

public class EntityUser : IdentityUser
  {
  }

Create a database context class derived from IdentityDbContext <EntityUser>:

public class AppDbContext : IdentityDbContext<EntityUser>
  {
public AppDbContext (DbContextOptions<AppDbContext> options)
         : base(options)
    {
    }
}

At last these documents may help you better understand identity scaffold : enter link description here enter link description here