On top of this code I want to seed more data into another table called Cities, How can I approach this problem?
I want to seed new data into the new table Cities
but I don't know where to place my code exactly, should I create another Seed
file ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Domain;
namespace Persistence
{
public class Seed
{
public static async Task SeedData(DataContext context)
{
if(context.Countries.Any()) return;
var countries = new List<Country>
{
new Country {
Name = "Albania",
CountryCode = "AL"
},
new Country {
Name = "France",
CountryCode = "FR"
},
new Country {
Name = "Germany",
CountryCode = "DE"
}
};
await context.Countries.AddRangeAsync(countries);
await context.SaveChangesAsync();
}
}
}
Same properties for Cities:
CityName
CityCode
Why not have seed method per entity? Here's what I mean:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Domain;
namespace Persistence
{
public class Seed
{
public static async Task SeedData(DataContext context)
{
await SeedCountries(context);
await SeedCities(context);
}
private static async Task SeedCountries(DataContext context)
{
if(context.Countries.Any()) return;
var countries = new List<Country>
{
new Country {
Name = "Albania",
CountryCode = "AL"
},
new Country {
Name = "France",
CountryCode = "FR"
},
new Country {
Name = "Germany",
CountryCode = "DE"
}
};
await context.Countries.AddRangeAsync(countries);
await context.SaveChangesAsync();
}
private static async Task SeedCities(DataContext context)
{
if(context.Cities.Any()) return;
var cities = new List<City> {
new City {
Name = "New York",
Code = "NYC"
},
new City {
Name = "Paris",
Code = "PAR"
},
new City {
Name = "Berlin",
Code = "BER"
}
};
await context.Cities.AddRangeAsync(cities);
await context.SaveChangesAsync();
}
}
}
Or use a more generic approach like this:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public static class DbSetExtensions
{
public static async Task SeedDataAsync<TEntity>(this DbSet<TEntity> dbSet, Func<IEnumerable<TEntity>> seedDataCreator) where TEntity : class
{
if (dbSet.Any())
{
return;
}
var data = seedDataCreator.Invoke();
await dbSet.AddRangeAsync(data);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Domain;
namespace Persistence
{
public class Seed
{
private async Task SeedDataAsync(DataContext db)
{
await db.Countries.SeedDataAsync(() => new[] {
new Country {
Name = "Albania",
CountryCode = "AL"
},
new Country {
Name = "France",
CountryCode = "FR"
},
new Country {
Name = "Germany",
CountryCode = "DE"
}
});
await db.Cities.SeedDataAsync(() => new[] {
new City {
Name = "New York",
Code = "NYC"
},
new City {
Name = "Paris",
Code = "PAR"
},
new City {
Name = "Berlin",
Code = "BER"
}
});
await db.SaveChangesAsync();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.