How to enable Migrations to Identity in Asp.net MVC

Asked

Viewed 355 times

1

I need to decouple Identity from my Asp.net core MVC layer and play it for another crosscutting layer of my DDD project. Based on Eduardo Pires' Equinox project, I was able to do it, but I’m finding it difficult to activate Migrations. So you’ll understand what I’ve done:

1 - I created the Commercial.Presentation.Web.MVC layer with user authentication. Automatically, the system created several folders, controllers, and classes needed for authentication to work, including the local database. Fantastic!!!!

inserir a descrição da imagem aqui

2 - I created a Commercial System layer.Infra.Crosscutting.Identity.Data and manually created the same folders, controllers and classes as the mvc layer. The idea is to activate Migrations on this layer and then I remove everything from the MVC layer and in this way, the operation would be decoupled and in the Identity layer.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

When I try to run "Add-Migration Initialcreate -Context: Applicationdbcontext", the following error msg appears:

Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0] User profile is available. Using 'C:* Local ASP.NET Dataprotection-Keys' as key Repository and Windows DPAPI to Encrypt Keys at Rest. More than one Dbcontext named 'Applicationdbcontext' was found. Specify which one to use by providing its Fully Qualified name using its Exact case.

 //Minha classe de contexto na camada Identity
 
 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // get the configuration from the app settings
            var config = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            // define the database to use
            optionsBuilder.UseNpgsql(config.GetConnectionString("DefaultConnectionpg"));
        }
        
    }

Does anyone know how to help me activate Migrations in the created Identity layer?

Project link: https://onedrive.live.com/? id=40838E65B9F8787E%21120&Cid=40838E65B9F8787E

1 answer

0


He’s complaining because you have two context with the same name (Applicationdbcontext.Cs) within your solution.

The solution is to delete your Context from your MVC layer or rename any of the contexts, since you specify the context name in the powershell line:

-Context: ApplicationDbContext
  • Hi Gabriel! I removed the context of MVC, but now it is giving this error: "Unable to create an Object of type 'Applicationdbcontext'. Add an implementation of 'Idesigntimedbcontextfactory<Applicationdbcontext>' to the project, or see https://go.microsoft.com/fwlink/? linkid=851728 for Additional Patterns supported at design time."

  • When executing the powershell, select the project where the context is.

  • I am selecting the project Systemscommercial.Infra.Crosscutting.Identity.Data, only that the error appears..

  • Share your context with us

  • I will include it in the post... Anything, if you want, you can download the project in the link above to look more detailed.... :)

  • https://github.com/aspnet/EntityFrameworkCore/issues/9415#issuecomment-327589912

  • I created the implementation class and the Migrations folder was enabled. Only when I give Update-Database, it gives this error: Keyword not supported: 'port'.

  • Add --output (seu diretório) just the front of the command.

  • My fault... It worked to give the Update... It was what I was doing for another database... He was trying to insert the tables in another kkkkkkk bank.

  • Thus: Add-Migration Initial_start -Context: Applicationdbcontext --output Data Migrations?

  • It’s what I’m doing for the Package Manager Console...

  • No package Manage console should use -Outputdir ... I got!

  • Post your reply Gabriel so that I mark it as answered... Thank you :)

  • We are commenting directly on my reply.

  • Right.. I hadn’t even noticed rsrsr... thanks bro :)

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.