我正在使用mvc模型创建一个ASP应用程序并通过我的类生成一个db现在它尝试使用DBInitializer.cs填充db。它表示它不能为标识列插入值。
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
公共静态类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 = "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");
}
}
答案 0 :(得分:2)
我看到您为每个团队指定了TeamID值。该列是Identity列吗?如果是这样,除非您启用了允许您使用的sql功能,否则无法为该列指定值。该功能称为Identity_Insert
。
最有可能的解决方案是不指定TeamID的值。