Я делаю ASP-приложение с моделью mvc и сгенерировал базу данных своими классами. Теперь он пытается заполнить базу данных DBInitializer.cs.Он говорит, что не может вставить значение для столбца идентификаторов.
введите описание изображения здесь A класс
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
открытый статический класс DbInitializer {public static void Initialize (контекст MotoContext) {
// 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 = "max@formule1.nl",
Address = "Amsterdam",
CountryID = 123,
RaceID = 1,
Number = 5,
OrderDate = DateTime.Parse("2017-01-24"),
Paid = false
},
new Ticket
{
Name = "Stef Wouters",
Email = "sw@ping.be",
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");
}
}