Reputation: 219
i have some difficulties with the following exception: "Object reference not set to an instance of an object."
Code-First Migration at EntityFramework MVC5
public class Driver
{
[Key]
public int NumberId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Age { get; set; }
public string TeamName { get; set; }
public Team Team { get; set; }
}
the Configuration:
namespace f1app.Migrations.F1app
{
using Data;
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<f1app.Data.F1appContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
MigrationsDirectory = @"Migrations\F1app";
}
protected override void Seed(f1app.Data.F1appContext context)
{
context.Teams.AddOrUpdate(
t => t.TeamName, DummyData.getTeams().ToArray());
context.SaveChanges();
context.Drivers.AddOrUpdate(
d => new { d.FirstName, d.LastName }, DummyData.getDrivers(context).ToArray());
}
}
}
and the Initial Create:
namespace f1app.Migrations.F1app
{
using System;
using System.Data.Entity.Migrations;
public partial class InitialCreate : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Drivers",
c => new
{
NumberId = c.Int(nullable: false, identity: true),
FirstName = c.String(),
LastName = c.String(),
Age = c.String(),
TeamName = c.String(maxLength: 30),
})
.PrimaryKey(t => t.NumberId)
.ForeignKey("dbo.Teams", t => t.TeamName)
.Index(t => t.TeamName);
CreateTable(
"dbo.Teams",
c => new
{
TeamName = c.String(nullable: false, maxLength: 30),
City = c.String(),
Founded = c.String(),
})
.PrimaryKey(t => t.TeamName);
}
public override void Down()
{
DropForeignKey("dbo.Drivers", "TeamName", "dbo.Teams");
DropIndex("dbo.Drivers", new[] { "TeamName" });
DropTable("dbo.Teams");
DropTable("dbo.Drivers");
}
}
}
than get some dummy data for the drivers to get into the table....
public static List<Driver> getDrivers(F1appContext context)
{
List<Driver> drivers = new List<Driver>(){
new Driver{
NumberId=5,
FirstName="Sebastian",
LastName="Vettel",
Age="29",
TeamName=context.Teams.Find("Ferrari").TeamName,
},
new Driver{
NumberId=44,
FirstName="Lewis",
LastName="Hamilton",
Age="32",
TeamName= context.Teams.Find("Mercedes").TeamName
},
new Driver{
NumberId=19,
FirstName="Felipe",
LastName="Massa",
Age="36",
TeamName=context.Teams.Find("Williams").TeamName
}
};
return drivers;
}
and using the PM Console for the Update - update-database -ConfigurationTypeName f1app.Migrations.F1app.Configuration
. But only the table for the teams work fine, the Drivers Table doesn't get any data.... Any idea why?
Upvotes: 0
Views: 440
Reputation: 7800
you have a missplaced
context.SaveChanges();
that is:
protected override void Seed(f1app.Data.F1appContext context)
{
context.Teams.AddOrUpdate(
t => t.TeamName, DummyData.getTeams().ToArray());
context.SaveChanges(); // <----------------------
context.Drivers.AddOrUpdate(
d => new { d.FirstName, d.LastName }, DummyData.getDrivers(context).ToArray());
//should be here:
context.SaveChanges(); // <----------------------
}
one is enough. I let both for the illustration.
Upvotes: 1