What i have here is a sample of the seed data that i wanna use to seed my db:
[
{
"City": "gqaka",
"AccentCity": "Gqaka",
"ProvinceName": "Eastern Cape",
"Latitude": -31.553917,
"Longitude": 28.210587,
"ProvinceID": 5
}
]
And my c# model currently looks like this:
public class Camp
{
public int Id { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public string Location { get; set; }
}
and my method for seed is this one :
public static async Task SeedCampgrounds(DataContext context)
{
if (await context.Campgrounds.AnyAsync()) return;
var campgroundData = await System.IO.File.ReadAllTextAsync("Data/cities.json");
var campgrounds = JsonSerializer.Deserialize<List<Camp>>(campgroundData);
foreach (var campground in campgrounds)
{
campground.Title = campground.Title.ToLower();
campground.Location = campground.Location.ToLower();
context.Campgrounds.Add(campground);
}
}
and in my program.cs i have //seed the database
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<DataContext>();
await context.Database.MigrateAsync();
await Seed.SeedCampgrounds(context);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occured while seeding the database.");
}
}
2