S
S
SokolovArtur2017-02-04 17:18:18
.NET
SokolovArtur, 2017-02-04 17:18:18

Insert into table during migration?

I deployed the project to .NET Core. In the first migration, I initialized the tables. Roles, rights, users. Now, using the same migration, I want to create a record in one of the tables. The problem is that in the body of the migration class, writing to the table is not possible.

namespace ApsNetCore.Data.Migrations
{
    public partial class CreateIdentitySchema : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oblomingo, 2017-02-04
@Oblomingo

You can add entries to the Seed method.
More or less like this:

protected override void Seed(BookService.Models.BookServiceContext context)
{
    context.Authors.AddOrUpdate(x => x.Id,
        new Author() { Id = 1, Name = "Jane Austen" },
        new Author() { Id = 2, Name = "Charles Dickens" },
        new Author() { Id = 3, Name = "Miguel de Cervantes" }
        );
}

Link to article:
https://www.asp.net/web-api/overview/data/using-we...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question