繁体   English   中英

为什么在C#EF中的Migration类中没有要覆盖的Seed方法?

[英]Why is there no Seed method to override in my Migration class in C# EF?

您好,我在编写此代码时未找到合适的方法来覆盖

受保护的重写void Seed(MuziekApp.MuziekContext上下文){}

为什么在迁移中没有覆盖的Seed方法? 我到底在想什么或做错了什么?

using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;

namespace MuziekApp.Migrations
{
    public partial class Second : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {

        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }

        protected override void Seed(MuziekApp.MuziekContext context)
        {

            List<Artiest> lijst = new List<Artiest>()
            {
                new Artiest() { ArtiestID = 1, Naam = "Jane", InstrumentID = 1, InstrumentNaam = "Saxofoon", PopgroepID = 1, GroepNaam = "Killers"},
                new Artiest() { ArtiestID = 2, Naam = "Charles", InstrumentID = 2, InstrumentNaam = "Gitaar", PopgroepID = 1, GroepNaam = "Killers"},
                new Artiest() { ArtiestID = 3, Naam = "Miguel", InstrumentID = 1, InstrumentNaam = "Saxofoon", PopgroepID = 2, GroepNaam = "Rock"},
                new Artiest() { ArtiestID = 4, Naam = "John", InstrumentID = 2, InstrumentNaam = "Gitaar", PopgroepID = 2, GroepNaam = "Rock" }
             };

            List<Instrument> lijst2 = new List<Instrument>()
            {
                new Instrument() {  InstrumentID = 1, InstrumentNaam = "Saxofoon"},
                new Instrument() {  InstrumentID = 2, InstrumentNaam = "Gitaar"}
             };

            List<Popgroep> lijst3 = new List<Popgroep>()
            {
                new Popgroep() { PopgroepID = 1, GroepNaam = "Killers"},
                new Popgroep() { PopgroepID = 2, GroepNaam = "Rock"}
             };

            context.Artiesten.AddRange(lijst);
            context.Instrumenten.AddRange(lijst2);
            context.Popgroepen.AddRange(lijst3);
        }
    }
}

您可以在迁移文件中使用Sql()

Sql("INSERT INTO Artiests (Name, InstrumentID) VALUES ('Jane', 1)");

它仅允许您使用SQL语句进行播种。 您可能希望先像仪器一样播种数据,以便该ID对您的FK有效。 只需考虑您输入的顺序即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM