How to enable Migrations using the Entity Framework Core = Windows Forms C #

1

I'm testing EF Core in a Windows forms project following the DDD standard. In the repository layer I created my context and in my domain I already have the defined Models classes. How do I activate Migrations in EF Core so that I do not have to create each migration manually and keep giving the updates? I wanted Migrations to be enabled and I just gave the update-database ...

public class PersistContext : DbContext
    {
        public PersistContext()
        {
        }

        public PersistContext(DbContextOptions<PersistContext> Options)
            :base(Options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ForSqlServerUseIdentityColumns();
            modelBuilder.HasDefaultSchema("SistemaComercial");

            PessoaTipoMap(modelBuilder);
        }

        public DbSet<PessoaTipo> PessoaTipo { get; set; }

        private void PessoaTipoMap(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PessoaTipo>(pt =>
            {
                pt.ToTable("tblPessoaTipo");

                pt.HasKey(ptk=> ptk.PessoaTipoId);
                pt.Property(ptp => ptp.PessoaTipoId)
                            .ValueGeneratedOnAdd();//Checar se o auto incremento funciona.

                pt.Property(ptp => ptp.Descricao)
                            .HasColumnName("Descricao")
                            .HasColumnType("Varchar")
                            .HasMaxLength(25)
                            .IsRequired();
            });
        }
    }

 public class FactoryPersistContext : IDbContextFactory<PersistContext>
    {
        private string ConnectionString = ConfigurationManager.ConnectionStrings["SistemaComercial"].ConnectionString;

        public PersistContext Create(DbContextFactoryOptions options)
        {
            var constructor = new DbContextOptionsBuilder<PersistContext>();
            constructor.UseSqlServer(ConnectionString);
            return new PersistContext(constructor.Options);
        }
    }
    
asked by anonymous 22.03.2017 / 00:33

1 answer

4

This option does not exist in the Entity Framework Core.

You can do something similar by having the migration done every time the application is started for development purposes.

Possibly if I were to use this option I would still do some checking to make sure the application is actually in development mode, but as I do not know the context I will not comment on it

Place this snippet in the application initialization code (probably in Startup.cs )

using (var contexto = new Contexto())
{
    contexto.Database.Migrate();
}
    
22.03.2017 / 03:05