-2

I am currently learning Entity Framework. To learn it better, I decided to create a car rental application, taking some ideas from expedia, etc.

At this point my program works, I can add, edit, delete from database, although my seed does not work.

I am open to any suggestions for improvement of this code, and how to fix issue I am experiencing.

Thanks

Link to repo: https://github.com/tharion85/Car-Rental

Done following this guide: https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

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();
    }
SteppingRazor
  • 1,222
  • 1
  • 9
  • 25
  • Thank you for that. I already updated customers with rentStart and rentEnd. Right now, i am experiencing error with The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Customers_dbo.Cars_Id". The conflict occurred in database "carRental1", table "dbo.Cars", column 'Id'. The statement has been terminated. Error at line 31: context.SaveChanges(); – Mat Kolodziej Nov 03 '16 at 22:20
  • I already told you. Add the car reference to the costumer. So for example `new Customer {Car = cars.First() ... }` – SteppingRazor Nov 04 '16 at 06:07