0
  public class InventarioContext : DbContext
{
    public InventarioContext() : base()
    {
        // I'm a default constructor!
    }

    public InventarioContext(DbContextOptions<InventarioContext> options) : base(options)
    {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }

    // Inventario Ciclico
    public DbSet<InventarioConfiguracao> InventarioConfiguracoes { get; set; }
    public DbSet<InventarioRegisto> InventarioRegistos { get; set; }
    public DbSet<Etiqueta> Etiquetas { get; set; }
    public DbSet<Localizacao> Localizacoes { get; set; }
    public DbSet<Armazem> Armazens { get; set; }
    public DbSet<Operador> Operadores { get; set; }
}

my second context

public class AbastecimentoContext : DbContext
{
    public AbastecimentoContext(DbContextOptions<AbastecimentoContext> options) : base(options)
    {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserRole>()
            .HasKey(ur => new { ur.UserId, ur.RoleId });

        modelBuilder.Entity<UserRole>()
            .HasOne(ur => ur.User)
            .WithMany(u => u.UserRoles)
            .HasForeignKey(ur => ur.UserId);

        modelBuilder.Entity<UserRole>()
            .HasOne(ur => ur.Role)
            .WithMany(r => r.UserRoles)
            .HasForeignKey(ur => ur.RoleId);

        modelBuilder.Entity<User>()
            .HasIndex(u => u.Username)
            .IsUnique();

        modelBuilder.Entity<Colaborador>()
            .HasIndex(c => c.Email)
            .IsUnique();

        modelBuilder.Entity<Role>()
            .HasIndex(r => r.Nome)
            .IsUnique();

        modelBuilder.Entity<Consumo>()
            .HasIndex(c => new { c.Referencia, c.UAP })
            .IsUnique();

        modelBuilder.Entity<Parametro>()
            .HasIndex(p => new { p.Referencia, p.UAP })
            .IsUnique();

        modelBuilder.Entity<Horario>()
            .HasIndex(h => new { h.Inicio, h.Fim, h.Uap })
            .IsUnique();

        modelBuilder.Entity<Feriado>()
            .HasIndex(f => f.DataInicio)
            .IsUnique();

        modelBuilder.Entity<Feriado>()
            .HasIndex(f => f.DataFim)
            .IsUnique();

        modelBuilder.Entity<Horario>()
            .HasIndex(h => h.IsDeleted)
            .HasName("XI_hHorarios_IsDeleted")
            .HasFilter("([IsDeleted]=(0))");

    }

    // Entitites
    public DbSet<Colaborador> Colaboradores { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<UserRole> UserRoles { get; set; }

    // Abastecimento
    public DbSet<Parametro> Parametros { get; set; }
    public DbSet<ListaAbastecimento> ListasAbastecimento { get; set; }
    public DbSet<Referencia> Referencias { get; set; }
    public DbSet<Consumo> Consumos { get; set; }
    public DbSet<Feriado> Feriados { get; set; }
    public DbSet<Horario> Horarios { get; set; }
    public DbSet<Uap> Uaps { get; set; }

    public DbSet<Calendario> Calendario { get; set; }
    public object InventarioConfiguracoes { get; internal set; }
}

as you can see there is no relation in any of them.

I even tried to remove the foreign key property

ColaboradorId on Inventariocontext

no matter what it won't let me create migration for InventarioContext, it keeps says missing primary key for UserRoles when it is already created from the other context.

Jackal
  • 3,359
  • 4
  • 33
  • 78
  • this may help. its about refencing foreign key property instead of navigation property when making composite key with fluent api. https://stackoverflow.com/questions/43711485/composite-key-ef-core-getting-error-when-using-fluent-api – Haithem KAROUI Aug 02 '19 at 10:27
  • I saw that post answer and i'm already doing that with UserRoles as fluent API on my AbastecimentoContext, either way the only solution i found out was to remove the other context and put it all in one – Jackal Aug 02 '19 at 10:30
  • that is not a solution, not even a workaround, it's a best practice to keep authentication and identity management in a separate context. since you have the chance of implementing it then do it correctly. the error should be about a small detail just don't abandon your best practices implementations to avoid an error. – Haithem KAROUI Aug 02 '19 at 10:37
  • I know that, I was actulally making a context for each Scheme, but i cannot wait for a solution because of deadlines – Jackal Aug 02 '19 at 11:13

0 Answers0