Your CarInitializer
class inherits from DropCreateDatabaseIfModelChanges
this means it will drop, create and run seed only if the model has changed. You can try it by for example adding another property to your Customer
model. If you do this the database will be recreated and seed will be executed.
If you want to always recreate the database inherit your class from DropCreateDatabaseAlways
so change this:
public class CarInitializer : DropCreateDatabaseIfModelChanges<CarContext>
to this:
public class CarInitializer : DropCreateDatabaseAlways<CarContext>
Also notice that your current seed won't run properly because Customer
depends on Car
so you first need to create a car and then add its reference to the customer. Also you forgot about rentStart
and rentEnd
properties. So for example, your seed can look like this:
protected override void Seed(CarContext context)
{
var cars = new List<Car>
{
new Car { Id=1033, Model="Corolla", Mark="Toyota", hasAC=true, rentalCost=65 }
};
cars.ForEach(ca => context.Cars.Add(ca));
var customers = new List<Customer>
{
new Customer {Car = cars.First(), FirstName="Carson", LastName="Alexander", Address="183 Court Road", ZipCode="T7D 0C1", City="Toronto", rentStart = DateTime.Now, rentEnd = DateTime.Now },
new Customer {Car = cars.First(), FirstName="Meredith", LastName="Alonso", Address="101 Baseline Rd", ZipCode="V4D 0G2", City="Vancouver", rentStart = DateTime.Now, rentEnd = DateTime.Now },
new Customer {Car = cars.First(), FirstName="Arturo", LastName="Brand", Address="1043 34st", ZipCode="T5Z 3P1", City="Calgary", rentStart = DateTime.Now, rentEnd = DateTime.Now }
};
customers.ForEach(c => context.Customers.Add(c));
context.SaveChanges();
}