ASP.Net MVC: Data
Asp.net
Core 2 MVC
If you
want to predefine tables in Asp.net core 2 MVC with values during the development
time, then you can fill them out using the migration.
In the
example, the selection table product groups with the selection values cordless
screwdrivers and drills is to be filled in advance.
Thus,
in the rollout of the project, these fixed tables are always the same.
To
populate the Migration Manager, enter in the Package Manager Console:
PM> add-migration
tblProduktgruppen_fuellen
|
This
will create a file in / migrations which will be an empty (..) Down (..)
Contains construct
In this
migration file you can now in the Up (..) Method write a SQL-Transact command.
As:
migrationBuilder.Sql("INSERT INTO Produktgruppen (Produktgruppe)
VALUES('Akku-Schrauber')");
migrationBuilder.Sql("INSERT INTO Produktgruppen (Produktgruppe)
VALUES('Bohrmaschinen')");
|
using
Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
namespace ArtikelWeb.Migrations
{
public partial class tblProduktgruppen_fuellen : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("INSERT INTO
Produktgruppen (Produktgruppe) VALUES('Akku-Schrauber')");
migrationBuilder.Sql("INSERT INTO
Produktgruppen (Produktgruppe) VALUES('Bohrmaschinen')");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}
|
To
transfer to the SQL Server database then use the Migration Update Database
command:
After
that, the data is in the SQL Server database