Run Insert in Migrations Entity Framework

Asked

Viewed 325 times

6

I am developing an ASP.NET MVC project with Entity Framework Code First. I already checked the bank and managed the first Migrations, my question is: has how to execute a insert right after creating a table? Example:

CreateTable(
            "dbo.Permissao",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Descricao = c.String(nullable: false, maxLength: 350),
                })
            .PrimaryKey(t => t.Id);
    //e aqui executar os insert
    //exemplo: insert into Permissao (id, descricao) values(1, "Administrador");
    //insert into Permissao (id, descricao) values(2, "Professor");
    //insert into Permissao (id, descricao) values(3, "Aluno");

1 answer

6


Yes, use the method Seed generated in Migrations\Configuratiom.cs:

    protected override void Seed(MeuProjeto.Models.ApplicationDbContext 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" }
        //    );
        //
    }

The Seed is executed at the end of the migration application process.

Browser other questions tagged

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