2
With automatic Migration the database is updated also when there is deletion of some property from my model class? That is, in addition to including a new attribute if something is added to my class, does it also remove? Like the example below, I’m trying to get him to remove the ZIP field that he’s been adding and it’s empty, but it’s not happening. Returns an exception Automaticdatalossexception
Beginning
static void Main(string[] args)
{
Aluno aluno = new Aluno
{
AlunoId = 1,
Nome = "Cláudia",
Email = "[email protected]"
};
using (var db = new DBContexto())
{
db.Aluno.Add(aluno);
db.SaveChanges();
}
}
Model Class
public class Aluno
{
public int AlunoId { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
//public string Cep { get; set; }
}
Context
public class DBContexto : DbContext
{
public DBContexto()
: base(@"data source=(local); initial catalog=DbAluno; integrated security=true;")
{
Database.SetInitializer<DBContexto>(new MigrateDatabaseToLatestVersion<DBContexto, Configuration>());
}
public DbSet<Aluno> Aluno { get; set; }
}
Configuration
public class Configuration : DbMigrationsConfiguration<ConsoleApplication1.DBContexto>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(ConsoleApplication1.DBContexto context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
It worked. Thank you!
– Kelly Soares