Doubt in the operation of Automatic Migration with Migratedatabasetolatestversion

Asked

Viewed 208 times

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

1 answer

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, it also removes?

Yes. The column is excluded.

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.

Yes, this is a protection of the Entity Framework so that important data is not lost. It is connected by default.

As you are in a development environment, you can include the following in the Migrations to avoid such verification:

internal sealed class Configuration : DbMigrationsConfiguration<MeuProjetoContext>
{
    public Configuration()
    {
        AutomaticMigrationDataLossAllowed = true;
    }

    ...
}
  • It worked. Thank you!

Browser other questions tagged

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