Seed İşlemleri
Veri tabanı yeni oluşturulacaksa otomatik olarak içerisine otomatik olarak kayıt ekler.
public static class DataSeeding
{
public static void Seed(DbContext context)
{
if(context.Database.GetPendingMigrations().Count()==0)
{
if(context is ShopContext)
{
ShopContext _context = context as ShopContext;
if(_context.Products.Count()==0)
{
_context.Products.AddRange(Products);
}
if(_context.Categories.Count()==0)
{
_context.Categories.AddRange(Categories);
}
}
context.SaveChanges();
}
}
private static Product[] Products = {
new Product{Name="Product Seed 1", Price=1},
new Product{Name="Product Seed 2", Price=2},
new Product{Name="Product Seed 3", Price=3},
new Product{Name="Product Seed 4", Price=4},
};
private static Category[] Categories = {
new Category{Name="Category Seed 1"},
new Category{Name="Category Seed 2"},
new Category{Name="Category Seed 3"},
new Category{Name="Category Seed 4"},
};
}
static void Main(string[] args)
{
DataSeeding.Seed(new ShopContext);
}
Last updated
Was this helpful?