0

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?

3
  • Initial guess would be that the context.Teams.Find("Ferrari").TeamName lines are failing due to the Find not returning any data. Are there Teams with these keys in the context? Could you also add your code for DummyData.getTeams() Commented Jun 27, 2017 at 15:18
  • your getTeams code is missing. Commented Jun 27, 2017 at 15:24
  • your TeamName=context.Teams.Find("Williams").TeamName is weird. TeamName="Williams" should be enough Commented Jun 27, 2017 at 15:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry, i've tried but nothing really has change, still the same error-exceptions after running the package manager console command - Object reference not set to an instance of an object.
thats the end of the msg i get from the pm console after running the update command: ..... at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) Object reference not set to an instance of an object.
I think the problem is somehow connected with the references or something.... dont know - trying to understand how the c# and .net MVC are working right now....
Sorry everyone.... found it - was some minor text error, missmash that i've done..... forget it - solved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.