2017-11-27 5 views
0

こんにちは、私はこのコードを書くときに上書きすることが見つかりませ適切な方法を受けていないよC#EFのマイグレーションクラスでオーバーライドするSeedメソッドがないのはなぜですか?

保護されたオーバーライド無効種子(MuziekApp.MuziekContextコンテキスト){}私はオーバーライドする種子メソッドを持っていない来るどのよう

私の移行?何が間違っているのか、間違っているのですか?

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); 
     } 
    } 
} 
+0

EFコアにはシードメソッドがありません。 – DavidG

+0

「Migration」のように、ドキュメントに記載された「Seed」メソッドがありません。https://docs.microsoft.com/en-us/ef/core/api/microsoft.entityframeworkcore.migration.migration – juharr

+0

次にシードメソッドを使用しますか? –

答えて

0

あなたはあなたの移行ファイル内のSQLを()を使用することができそれはちょうどあなたが播種用のSQL文を使用することができます

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

。あなたのインストゥルメントのようなデータを最初にシードしてIDがあなたのFKに有効であるようにしたいと思うでしょう。

+0

これはどのメソッドで行うのですか –

+0

変更は 'Up()'になります。ダウングレードで変更を行う場合は、それらを 'Down()'に入れることができます。 – James

関連する問題