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