0
votes

I am making an ASP application with mvc model and generated a db by my classes Now it try to fill the db with a DBInitializer.cs. It says that it cannot insert a value for identity column.

enter image description here A class

public class Rider
{
    public int RiderID { get; set; }
    public String LastName { get; set; }
    public String FirstName { get; set; }
    public int CountryID { get; set; }
    public int TeamID { get; set; }
    public String Bike { get; set; }
    public int Number { get; set; }
    public Team Team { get; set; }
    public Country Country { get; set; }

}

DBInitializer.cs

public static class DbInitializer { public static void Initialize(MotoContext context) {

        // Look for any countries.
        if (context.Countries.Any())
        {
            return;   // DB has been seeded

        }
            context.Countries.AddRange(

            new Country { Name = "White Russia", CountryID = 189 },
            new Country { Name = "Yemen", CountryID = 190 },
            new Country { Name = "Zaire", CountryID = 191 },
            new Country { Name = "Zambia", CountryID = 192 },
            new Country { Name = "Zimbabwe", CountryID = 193 }
            );
            context.SaveChanges();

            context.Teams.AddRange(
                new Team { TeamID = 1, Name = "Avintia Racing", Logo = "Avintia.PNG" },
                new Team { TeamID = 2, Name = "Cardion AB Motoracing", Logo = "AB.PNG" },

                );
            context.SaveChanges();

            context.Races.AddRange(





                new Race { Country = "Spain", Name = "Comunitat Valenciana", Description = "The Circuito de la Comunitat Valenciana was completed in 1999 and held rounds of the MotoGP and Spanish Motorcycle Championships in the same year. The Cheste track has several layouts, running anti-clockwise with varying lengths. MotoGP events are held on a 4km track comprising of five right handed corners, eight left handers and a 650m straight. Although the track is regarded as quite small, the pit complex contains 48 garages whilst the stadium style grandstands can seat up to 150,000 spectators. The circuit layout which allows all parts of the circuit to be seen from any stand helps to create a unique atmosphere enjoyed by Spanish and international riders alike and as the last race of the season there is always a party feeling to the Grand Prix, which was voted best GP of 2005 by IRTA.", Length = 4005, Date = DateTime.Parse("2017-06-09"), X = 649, Y = 363 }
                );
            context.SaveChanges();

            context.Riders.AddRange(

                new Rider { LastName = "Bradl", FirstName = "Stefan", CountryID = 62, TeamID = 4, Bike = "Honda", Number = 6 },
                new Rider { LastName = "Pedrosa", FirstName = "Dani", CountryID = 159, TeamID = 5, Bike = "Honda", Number = 26 },
                new Rider { LastName = "Marquez", FirstName = "Marc", CountryID = 159, TeamID = 5, Bike = "Honda", Number = 93 }
                );
            context.SaveChanges();

            context.Tickets.AddRange(
                new Ticket
                {
                    Name = "Max Verstappen",
                    Email = "[email protected]",
                    Address = "Amsterdam",
                    CountryID = 123,
                    RaceID = 1,
                    Number = 5,
                    OrderDate = DateTime.Parse("2017-01-24"),
                    Paid = false
                },
                new Ticket
                {
                    Name = "Stef Wouters",
                    Email = "[email protected]",
                    Address = "Brussels",
                    CountryID = 15,
                    RaceID = 1,
                    Number = 3,
                    OrderDate = DateTime.Parse("2017-01-23"),
                    Paid = true
                }
              );
            context.SaveChanges();
        }
    }

MotoContext.cs

public class MotoContext : DbContext
{
    public MotoContext(DbContextOptions<MotoContext> options ) : base(options)
    {

    }
    public DbSet<Team> Teams { get; set; }
    public DbSet<Rider> Riders { get; set; }
    public DbSet<Country> Countries { get; set; }
    public DbSet<Ticket> Tickets { get; set; }
    public DbSet<Race> Races { get; set; }


    protected override void OnModelCreating (ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Team>().ToTable("Team");

        modelBuilder.Entity<Rider>().ToTable("Rider");
        modelBuilder.Entity<Country>().ToTable("Country");
        modelBuilder.Entity<Ticket>().ToTable("Ticket");
        modelBuilder.Entity<Race>().ToTable("Race");
    }
}
1
Without seeing your schema, it's hard to be 100% certain, but I think probably the TeamID and/or CountryID (and possible the primary key fields on other entities) are not yours to specify. The database will generate the ID (and ensure it's unique) for a row whenever you insert a new record, and then allocate it to your data. Remove the bits of code where you are trying to set primary key IDs manually and wait for the database to do its job. - ADyson

1 Answers

2
votes

I see that you are specifying a TeamID value for each Team. Is that column an Identity column? If so, you cannot specify values for that column unless you enable the sql feature that let's you. The feature is called Identity_Insert.

Most likely, the solution is not to specify the value for TeamID.