在我的普通.NET框架中,我在Configuration.cs中使用了以下代码:
protected override void Seed(Athlosify.Models.AthlosifyContext context)
{
var golfCourses = new List<GolfCourse>
{
new GolfCourse { Name = "Bonnie Doon Golf Club", Description = "N/A", Address = "38 Banks Ave", Suburb = "Pagewood", State="NSW", Postcode = "2035", Country = "Australia", Latitude=-33.935189, Longitude=151.222372 },
new GolfCourse { Name = "Beverly Park Golf Club", Description = "N/A", Address = "87 Jubilee Avenue", Suburb = "Beverly Park", State="NSW", Postcode = "2217", Country = "Australia", Latitude=-33.975506, Longitude=151.131691 }
};
golfCourses.ForEach(s => context.GolfCourses.AddOrUpdate(p => p.GolfCourseID, s));
context.SaveChanges();
var golfRoundCategories = new List<GolfRoundCategory>
{
new GolfRoundCategory { Name = "Competition - Stroke", Description = "N/A" },
new GolfRoundCategory { Name = "Competition - Stableford", Description = "N/A" },
new GolfRoundCategory { Name = "Competition - Ambrose", Description = "N/A" },
new GolfRoundCategory { Name = "Competition - Foresome", Description = "N/A" },
new GolfRoundCategory { Name = "Practice - 2 Ball Best Balls", Description = "N/A" },
new GolfRoundCategory { Name = "Practice - 2 Ball Worst Balls", Description = "N/A" },
};
golfRoundCategories.ForEach(s => context.GolfRoundCategories.AddOrUpdate(p => p.GolfRoundCategoryID, s));
context.SaveChanges();
var golfRounds = new List<GolfRound>
{
new GolfRound { Name = "Bonnie Doon GC Practice", Notes = "N/A", UserID = Guid.Parse("7dd950ad-d7b0-448a-b689-db3947e31bec"),
GolfCourseID = golfCourses.Single(c => c.Name == "Bonnie Doon Golf Club" ).GolfCourseID,
DailyHandicap = 21, GolfRoundCategoryID = golfRoundCategories.Single(c => c.Name == "Practice - 2 Ball Best Balls" ).GolfRoundCategoryID,
TeeOffUTC = DateTime.Parse("2018-07-22 15:00") },
};
golfRounds.ForEach(s => context.GolfRounds.AddOrUpdate(p => p.GolfRoundID, s));
context.SaveChanges();
尝试在.Net Core 2.1中进行播种,然后再获取该值:
GolfCourseID = golfCourse.Single(c => c.Name ==“ Bonnie Doon高尔夫俱乐部”).GolfCourseID
和
GolfRoundCategoryID = golfRoundCategories.Single(c => c.Name ==“ Practice-2 Ball Best Balls”)。GolfRoundCategoryID
?!!?!
public static void Seed(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<GolfCourse>().HasData(
new GolfCourse { Name = "Bonnie Doon Golf Club", Description = "N/A", Address = "38 Banks Ave", Suburb = "Pagewood", State = "NSW", Postcode = "2035", Country = "Australia", Latitude = -33.935189, Longitude = 151.222372 },
new GolfCourse { Name = "Beverly Park Golf Club", Description = "N/A", Address = "87 Jubilee Avenue", Suburb = "Beverly Park", State = "NSW", Postcode = "2217", Country = "Australia", Latitude = -33.975506, Longitude = 151.131691 }
);
modelBuilder.Entity<GolfRoundCategory>().HasData(
new GolfRoundCategory { Name = "Competition - Stroke", Description = "N/A" },
new GolfRoundCategory { Name = "Competition - Stableford", Description = "N/A" },
new GolfRoundCategory { Name = "Competition - Ambrose", Description = "N/A" },
new GolfRoundCategory { Name = "Competition - Foresome", Description = "N/A" },
new GolfRoundCategory { Name = "Practice - 2 Ball Best Balls", Description = "N/A" },
new GolfRoundCategory { Name = "Practice - 2 Ball Worst Balls", Description = "N/A" }
);
modelBuilder.Entity<GolfRound>().HasData(
new GolfRound
{
Name = "Bonnie Doon GC Practice",
Notes = "N/A",
UserID = Guid.Parse("7dd950ad-d7b0-448a-b689-db3947e31bec"),
GolfCourseID = golfCourse.Single(c => c.Name == "Bonnie Doon Golf Club").GolfCourseID,
DailyHandicap = 21,
GolfRoundCategoryID = golfRoundCategories.Single(c => c.Name == "Practice - 2 Ball Best Balls").GolfRoundCategoryID,
TeeOffUTC = DateTime.Parse("2018-07-22 15:00")
},
);
}
答案 0 :(得分:1)
在EF Core中,种子数据是迁移的组成部分。就像数据库架构更改一样,种子数据的更改也会被检测到并嵌入到迁移中。这样,就没有 DbContext 可以运行查询或检索由数据库引擎自动生成的ID。
您需要为自动生成的键列设置显式ID值,以便能够引用这些实体:
var golfCourse1 = new GolfCourse { GolfCourseID = 1, Name = "Bonnie Doon Golf Club", Description = "N/A", Address = "38 Banks Ave", Suburb = "Pagewood", State = "NSW", Postcode = "2035", Country = "Australia", Latitude = -33.935189, Longitude = 151.222372 };
modelBuilder.Entity<GolfCourse>().HasData(
golfCourse1,
// ...
);
var golfRoundCategory5 = new GolfRoundCategory { GolfRoundCategoryID = 5, Name = "Practice - 2 Ball Best Balls", Description = "N/A" };
modelBuilder.Entity<GolfRoundCategory>().HasData(
// ...
golfRoundCategory5,
// ...
);
modelBuilder.Entity<GolfRound>().HasData(
new GolfRound
{
Name = "Bonnie Doon GC Practice",
Notes = "N/A",
UserID = Guid.Parse("7dd950ad-d7b0-448a-b689-db3947e31bec"),
GolfCourseID = golfCourse1.GolfCourseID,
DailyHandicap = 21,
GolfRoundCategoryID = golfRoundCategory5.GolfRoundCategoryID,
TeeOffUTC = DateTime.Parse("2018-07-22 15:00")
},
);