Code first Migrations does not work

Asked

Viewed 728 times

0

I did it in a separate layer called Data

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);
    }

When executing the command Enable-Migrations no error. But the database and tables were not created.

1 answer

3


The command Enable-Migrations only activates the migration resource.

To update the database you need to create a Migration and update the base by running the following commands on Package Manager Console.

Create the Migration

Add-Migration Nomedamigration

Update the base

Update-Database


It is also possible to update the bank without creating Migrations, this is useful at development time, when the bank changes very quickly and very often.

When rotating the command Enable-Migrations a folder will be created Migrations and within it a class Configuration, within that class it is possible to change this setting.

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

With this, from then only need to run the command Update-Database to update the base.

Browser other questions tagged

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