Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seed method not called, Entity Framework 6

I have a DatabaseInitializer class

public class DatabaseInitializer : CreateDatabaseIfNotExists<DatabaseContext>
{
    protected override void Seed
        (
        DatabaseContext databaseContext
        )
    {
        // Seed the hash methods.
        var defaultHashMethod = new HashMethod
        {
            Description = "Default",
            CreateDate = DateTime.Now
        };

        databaseContext.HashMethod.Add(defaultHashMethod);

        databaseContext.SaveChanges();
    }
}

In my DatabaseContext class I set the initializer

public DatabaseContext() : base("DatabaseContext")
{
    InitializeDatabase();
}

private void InitializeDatabase()
{
    Database.SetInitializer(new DatabaseInitializer());
    if (!Database.Exists())
    {
        Database.Initialize(true);
    }            
}

As far as I can understand the seed method is only invoked once you perform an operation such as a query. My database is created successfully and I'm querying the table, but the seed method is never called.

Update:

It seems like the problem is caused because of a class that is inheriting from my DatabaseContext class, when using this class to perform database operations, the seed method is not called. When using my DatabaseContext class, everything works as expected

public DbSet<TestEntity> TestEntity { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
like image 745
Andre Lombaard Avatar asked Aug 27 '14 10:08

Andre Lombaard


People also ask

What is a Seed method?

The Stakeholder Engagement in Question Development and Prioritization method, better known as the SEED Method, is a process to engage community stakeholders in developing research questions and action plans on health-related topics.

What is seed method in MVC?

This seed() method in configuration. cs is called when you run update-database in the Package Manager Console. It's also called at application startup if you change Entity Framework to use the MigrateDatabaseToLatestVersion database initializer.

What is Seed data in database?

Data seeding is the process of populating a database with an initial set of data. There are several ways this can be accomplished in EF Core: Model seed data. Manual migration customization. Custom initialization logic.


1 Answers

You need to call Update-Database from the Package Manager Console.

like image 131
jumuro Avatar answered Sep 19 '22 23:09

jumuro