Code first migrations does not work

0

I made a separate layer called 'Date'

public class Context : DbContext
{
    public Context() : base("EscolaContext")
    {
    }

    public DbSet<Escola> Escolas { get; set; }
    public DbSet<Turma> Turmas { get; set; }
    public DbSet<Aluno> Alunos { get; set; }
    public DbSet<Usuario> Usuarios { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Escola>().HasKey(c => c.Id);
        modelBuilder.Entity<Escola>().Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<Escola>().Property(c => c.Nome).HasMaxLength(100);
        modelBuilder.Entity<Escola>().Property(c => c.Cnpj).HasMaxLength(14);

        modelBuilder.Entity<Turma>().HasKey(c => c.Id);
        modelBuilder.Entity<Turma>().Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<Turma>().Property(c => c.Nome).HasMaxLength(100);

        modelBuilder.Entity<Aluno>().HasKey(c => c.Id);
        modelBuilder.Entity<Aluno>().Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<Aluno>().Property(c => c.Nome).HasMaxLength(100);
        modelBuilder.Entity<Aluno>().Property(c => c.Matricula).HasMaxLength(20);

        modelBuilder.Entity<Usuario>().HasKey(c => c.Id);
        modelBuilder.Entity<Usuario>().Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<Usuario>().Property(c => c.Login).HasMaxLength(20);
        modelBuilder.Entity<Usuario>().Property(c => c.Senha).HasMaxLength(8);

        modelBuilder.Entity<Turma>().HasRequired(c => c.Escola).WithMany(p => p.Turmas).HasForeignKey(p => p.IdEscola);
        modelBuilder.Entity<Aluno>().HasRequired(c => c.Turma).WithMany(p => p.Alunos).HasForeignKey(p => p.IdTurma);
    }

Running the command Enable-Migrations did not give an error. But the bank and the tables were not created.

    
asked by anonymous 23.02.2017 / 18:11

1 answer

3

The Enable-Migrations command only activates the migration feature.

To upgrade the database, you need to create a migration and upgrade the database by running the following commands in the Package Manager Console .

Create the migration

  

Add-MigrationMigrationName

Update the database

  

Update-Database

You can also upgrade the database without creating migrations , which is useful at development time when the database changes very fast and very often.

When you run the command Enable-Migrations a Migrations folder will be created and within it a Configuration class, within this class you can change this setting.

// Construtor da classe Configuration
public Configuration()
{
    AutomaticMigrationsEnabled = true;
}

With this, you only have to run the Update-Database command to update the database.

    
23.02.2017 / 18:17