2016-05-12 8 views
1

私は選手がいるサッカーチームとデータベースを作成しています。データを追加するためにシードメソッドを使用していますが、Entityフレームワークがデータベースを更新しないため、オブジェクト参照がオブジェクトのインスタンスに設定されていません。エラー。シードメソッドは外部キーオブジェクトを追加できません

シードメソッドでリレーションをリンクする方法がわかりません。以下はコードです。


シード方法

protected override void Seed(DataLayer.FootballContext context) 
{ 
     List<VoetbalPloeg> VoetbalPloegModels = new List<VoetbalPloeg>(); 

     VoetbalPloegModels.Add(new VoetbalPloeg() 
     { 
      PloegNaam = "FC Barcelona", 
      StamNummer = 1241, 
      Spelers = 
      { 
       new Speler() { VoorNaam = "Lionel", AchterNaam = "Messi", Assists = 2, Goals = 15, GeboorteDatum = new DateTime(1990, 05, 15), Positie = "Spits", Rugnummer = 10}, 
       new Speler() { VoorNaam = "Alonso", AchterNaam = "Xabi", Assists = 3, Goals = 7, GeboorteDatum = new DateTime(1985, 04, 24), Positie = "Centraal", Rugnummer = 13} 
      } 
     }); 

     VoetbalPloegModels.Add(new VoetbalPloeg() 
     { 
      PloegNaam = "Real Madrid", 
      StamNummer = 1546, 
      Spelers = 
      { 
       new Speler() { VoorNaam = "Cristano", AchterNaam = "Ronaldo", Assists = 0, Goals = 17, GeboorteDatum = new DateTime(1989, 10, 7), Positie = "Spits", Rugnummer = 7}, 
       new Speler() { VoorNaam = "Sergio", AchterNaam = "Ramos", Assists = 0, Goals = 4, GeboorteDatum = new DateTime(1986, 12, 04), Positie = "LaatsteMan", Rugnummer = 10} 
      } 
     }); 

     foreach (var item in VoetbalPloegModels) 
      context.VoetbalPloeg.Add(item); 

     base.Seed(context); 
} 

プレーヤーモデル

public class Speler 
{ 
    [Key] 
    public int SpelerId { get; set; } 
    public string VoorNaam { get; set; } 
    public string AchterNaam { get; set; } 
    public int Rugnummer { get; set; } 
    public string Positie { get; set; } 
    public int Goals { get; set; } 
    public int Assists { get; set; } 
    public DateTime GeboorteDatum { get; set; } 

    // Forgein Key 
    // public int VoetbalPloegId { get; set; } 

    // Navigation Property 
    //public virtual VoetbalPloeg VoetbalPloeg { get; set; } 

} 

サッカーチームモデル

public class VoetbalPloeg 
{ 
    [Key] 
    public int VoetbalPloegId { get; set; } 
    public string PloegNaam { get; set; } 
    public int StamNummer { get; set; } 

    public virtual ICollection<Speler> Spelers { get; set; } 
} 

EDIT:答えとしてマークされた質問は、あなたがグーグルに入る最初の結果であり、あなたが私の名前を入力するとき一般的なエラーメッセージ。

+0

? –

+0

なしPMコンソールでupdate-databaseを使用しています –

+0

[Entity Framework MigrationでのNull参照]の重複の可能性あり(http://stackoverflow.com/questions/9481784/null-reference-onentity-framework-migration) – Gusman

答えて

1

あなたは、移行フォルダの使用であなたのConfiguration.csからシードを実行している場合:

public Configuration() 
{ 
    AutomaticMigrationsEnabled = true; 
} 

protected override void Seed(TestContext context) 
    { 
     // Where 'VoetbalPloegs' is the name of your table in the dbcontext 

     context.Spelers.AddOrUpdate(x => x.Id, 
      new Speler() { Id = 1, VoorNaam = "Lionel", AchterNaam = "Messi", Assists = 2, Goals = 15, GeboorteDatum = new DateTime(1990, 05, 15), Positie = "Spits", Rugnummer = 10, VoetbalPloegId = 1 }, 
      new Speler() { Id = 2, VoorNaam = "Alonso", AchterNaam = "Xabi", Assists = 3, Goals = 7, GeboorteDatum = new DateTime(1985, 04, 24), Positie = "Centraal", Rugnummer = 13, VoetbalPloegId = 1}, 
      new Speler() { Id = 3, VoorNaam = "Cristano", AchterNaam = "Ronaldo", Assists = 0, Goals = 17, GeboorteDatum = new DateTime(1989, 10, 7), Positie = "Spits", Rugnummer = 7, VoetbalPloegId = 2 }, 
      new Speler() { Id = 4, VoorNaam = "Sergio", AchterNaam = "Ramos", Assists = 0, Goals = 4, GeboorteDatum = new DateTime(1986, 12, 04), Positie = "LaatsteMan", Rugnummer = 10, VoetbalPloegId = 2} 
      ); 

     context.VoetbalPloegs.AddOrUpdate(x => x.Id, 
      new VoetbalPloeg() { Id = 1, PloegNaam = "FC Barcelona", StamNummer = 1241 }, 
      new VoetbalPloeg() { Id = 2, PloegNaam = "Real Madrid", StamNummer = 1546 } 
      ); 
    } 

(上記のように)trueに設定してください自動移行を行います。

Spelerエンティティ:

public class Speler 
{ 
    public int Id { get; set; } 
    public string VoorNaam { get; set; } 
    public string AchterNaam { get; set; } 
    public int Rugnummer { get; set; } 
    public string Positie { get; set; } 
    public int Goals { get; set; } 
    public int Assists { get; set; } 
    public DateTime GeboorteDatum { get; set; } 

    // Forgein Key 
    public int VoetbalPloegId { get; set; } 
} 

VoetbalPloegエンティティ:DbContextで

public class VoetbalPloeg 
{ 
    public int Id { get; set; } 
    public string PloegNaam { get; set; } 
    public int StamNummer { get; set; } 

    public virtual ICollection<Speler> Spelers { get; set; } 
} 

追加:例外がスローされたライン

public DbSet<Speler> Spelers { get; set; } 
    public DbSet<VoetbalPloeg> VoetbalPloegs { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 

     base.OnModelCreating(modelBuilder); 

     modelBuilder.Entity<VoetbalPloeg>().HasMany(s => s.Spelers).WithRequired().HasForeignKey(h => h.VoetbalPloegId); 

    } 
+0

まだエラーメッセージが表示される可能性がありますか? –

+0

は機能しますが、プレイヤーとチームの間にリンクはありません –

+0

@Mercenary_Frank新しいナビゲーションプロパティを追加public virtual VoetbalPloeg VoetbalPloeg {get; set;}をSpelerクラスに追加します。 – yyou

関連する問題