Execute insert in Migrations Entity Framework

6

I'm developing an ASP.NET MVC project with Entity Framework Code First. I've mapped the bank and managed the first Migrations, my question is: can I run a insert shortly after a table is created? 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");
    
asked by anonymous 06.01.2016 / 23:08

1 answer

6

Yes, use the Seed method 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 runs at the end of the migration process.

    
06.01.2016 / 23:19