Biacki
Biacki

Reputation: 21

Database initializer not seeding

I have been looking and looking for an answer, and there are multiple but I cant get it to work from the solutions that work on those questions. My problem is that the data is not seeded to the database. the database works and the tables are created but the data from the database initializer is not put into it.

Context class

using System.Data.Entity;

namespace FlextrafikWeb.Models
{
    public class Context : DbContext
    {
        public Context() : base("FlextrafikWeb") { }

        public DbSet<Car> Cars { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<Price> Prices { get; set; }
    }

}

Database initializer

using System.Collections.Generic;
using System.Data.Entity;

namespace FlextrafikWeb.Models
{

    public class DatabaseInitializer :     DropCreateDatabaseIfModelChanges<Context>
    {
        protected override void Seed(Context context)

        {
            GetUsers().ForEach(u => context.Users.Add(u));
            GetCars().ForEach(c => context.Cars.Add(c));
            GetLocations().ForEach(l => context.Locations.Add(l));
            GetPrices().ForEach(p => context.Prices.Add(p));

            context.SaveChanges();
        }

        private static List<Car> GetCars()
        {
            var cars = new List<Car>
        {
            new Car
            {
                CarID = 1,
                GaranteeCarNumber = 2,
                RegistrationNumber = 123456,
                ContactTelephone = "12 34 56 78",
                CarType = 1,
                CarLocation = null,
                CarPrice = null,
                CarOwner = null,
                UserID = 1,
            },

            new Car
            {
                CarID = 3,
                GaranteeCarNumber = 4,
                RegistrationNumber = 654321,
                ContactTelephone = "87 65 43 21",
                CarType = 2,
                CarLocation = null,
                CarPrice = null,
                CarOwner = null,
                UserID = 2,
            },

        };

            return cars;
        }

        private static List<User> GetUsers()
        {
            var users = new List<User>
        {
            new User
            {
                UserID = 1,
                UserName = "Firma A",
                CompanyCVR = 12345678
            },

            new User
            {
                UserID = 2,
                UserName = "Firma B",
                CompanyCVR = 98765432
            }
        };

            return users;
        }

        private static List<Location> GetLocations()
        {
            var locations = new List<Location>
        {
            new Location
            {
                LocationID = 1,
                StreetName = "Testgade",
                StreetNumber = 4,
                PostalCode = 5000,
                City = "Odense",
                Municipality = "Odense",
            },

              new Location
            {
                LocationID = 2,
                StreetName = "Testvej",
                StreetNumber = 34,
                PostalCode = 5300,
                City = "Kerteminde",
                Municipality = "Kerteminde",
            }
        };

            return locations;
        }

        private static List<Price> GetPrices()
        {
            var prices = new List<Price>
        {
            new Price
            {
                PriceID = 1,
                StartupFeeWeekday = 30.00,
                RunningRateWeekday = 330.00,
                WaitingRateWeekday = 250.00,
                StartupFeeEvening = 60.00,
                RunningRateEvening = 500.00,
                WaitingRateEvening = 400.00,
                StartupFeeWeekend = 600.00,
                RunningRateWeekend = 500.00,
                WaitingRateWeekend = 400.00,
            },

                new Price
            {
                PriceID = 2,
                StartupFeeWeekday = 10.00,
                RunningRateWeekday = 249.00,
                WaitingRateWeekday = 249.00,
                StartupFeeEvening = 20.00,
                RunningRateEvening = 269.00,
                WaitingRateEvening = 269.00,
                StartupFeeWeekend = 20.00,
                RunningRateWeekend = 269.00,
                WaitingRateWeekend = 269.00,
            },
        };

            return prices;

        }
    }
}

Global

    using System.Web.Security;
using System.Web.SessionState;
using System.Data.Entity;
using FlextrafikWeb.Models;

namespace FlextrafikWeb
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Initialize the product database.
            Database.SetInitializer(new DatabaseInitializer());


        }
    }
}

I don't know what else you would need, but nothing I seem to be able to come up with works.

Upvotes: 2

Views: 362

Answers (2)

Omar.Alani
Omar.Alani

Reputation: 4130

Two things here:

  1. Database initializer only when the database recreated, in your case your initializer will kick off when your datamodel changes, to recreate and seed your database always, change your initializer to inherit from dropcreate database always initalizer.
  2. You need to set your initialiser specifically in your dbcontect constructor (most likely through static constructor)

This tutorial should give you more details http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx

Hope this helps

Upvotes: 2

mcserep
mcserep

Reputation: 3299

You should set the using of your custom initializer in the constructor of the context class:

using System.Data.Entity;

namespace FlextrafikWeb.Models
{
    public class Context : DbContext
    {
        public Context() : base("FlextrafikWeb")
        {
            Database.SetInitializer(new DatabaseInitializer());
        }

        public DbSet<Car> Cars { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<Price> Prices { get; set; }
    }
}

Upvotes: 1

Related Questions