Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Seed method is not being called

We are using Entity Framework 4.4 and using migrations. The database already exists and we need to update it on regular basis. The seed method, however, is not being called and so lookup values are not being added.

The code looks as follow:

internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
{
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            SetSqlGenerator("System.Data.SqlClient", new OurSqlServerMigrationSqlGenerator());
        }

        protected override void Seed(KinectionDbContext context)
        {
            SeedLookupTables(context);
        }

        private static void SeedLookupTables(KinectionDbContext context)
        {
            context.Titles.AddOrUpdate(t => t.Value,
                new Title {Value = "Mr"},
                new Title {Value = "Mrs"},
                new Title {Value = "Miss"},
                new Title {Value = "Ms"},
                new Title {Value = "Dr"}
            );

            context.SaveChanges();
        }

}


public class MyDbContext : ObjectContext
    {
        public MyDbContext()
        {

        }

        static MyDbContext ()
        {
            Database.SetInitializer<KinectionDbContext>(null);
        }

        public DbSet<Title> Titles { get; set; }

}

And we are calling:

Add-Migration Seed

But the migration comes up empty.

Does anyone has an idea why the Seed is cmot being called and why the additional values in the lookup table are not being detected?

Thanks N

like image 665
Johnny Doeness Avatar asked Nov 27 '13 14:11

Johnny Doeness


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

The Migrations Seed method

runs whenever the Update-Database PowerShell command is executed

You need to call Update-Database not Add-Migration

Add-Migration scaffolds a migration file containing commands to migrate the database to a new version. It is empty because there are no schema changes to make. You do not need to call Add-Migration before calling Update-Database if all you want to do is seed

References:

Code first Db initialization strategies.
Code first migrations recommended reading
Managed Migrations
Database initializer and Migrations Seed methods

like image 149
Colin Avatar answered Nov 03 '22 01:11

Colin