Peter Kellner
Peter Kellner

Reputation: 15478

seed method not called with EntityFramework CodeFirst

I've been struggling on and off with this problem since 4.1 (now I'm on 4.3). It seems to me that to get the seed method called, all I should have to do is the following:

1) Create an empty data catalog on sqlserver 2) Execute the code below:

Database.SetInitializer(new DropCreateDatabaseAlways<SiteDB>());

I have my SiteDB defined as follows:

public class SiteDBInitializer : 
    DropCreateDatabaseAlways<SiteDB>
{
    protected override void Seed(SiteDB db)
    {
           ... (break point set here that never gets hit)

I feel like I must be missing something very simple because this creates my tables, but does never calls the seed method.

To Make this more clear, here is a full example that includes all the code. When I run it, seed never gets called:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace ConApp
{
internal class Program
{
    private static void Main(string[] args)
    {
        Database.SetInitializer(new SiteDBInitializer());
        using (var db = new SiteDB())
        {
            var x = db.Customers;
        }
    }
}

public class SiteDB : DbContext
{
    public DbSet<Customer> Customers { get; set; }
}

public class Customer
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }

    public string LastName { get; set; }
}

public class SiteDBInitializer :
    DropCreateDatabaseAlways<SiteDB>
{
    protected override void Seed(SiteDB db)
    {
        db.Customers.Add(new Customer() {LastName = "Kellner"});
        db.Customers.Add(new Customer() {LastName = "Jones"});
        db.Customers.Add(new Customer() {LastName = "Smith"});
        db.SaveChanges();
    }
}

}

Upvotes: 15

Views: 16113

Answers (2)

Roni Axelrad
Roni Axelrad

Reputation: 439

I looked at all the answers for that, nothing really works, and I wonder if that's a Microsoft bug for not calling the Seed method when DB does not exists. The only code that worked, was to actually make the class call the seed if DB does not exists:

Context class:

class AlisDbContext : DbContext
{
    public class MyContextFactory : IDbContextFactory<AlisDbContext>
    {
        public AlisDbContext Create()
        {
            return new AlisDbContext("CompactDBContext");
        }
    }

    public AlisDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
    {
        Database.SetInitializer(new AlisDbInitializer(this));
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AlisDbContext>());
    }

    public DbSet<SavedCredentials> SavedCredentialses { get; set; }
}

Then AlisDbInitializer need to check and call the seed method like:

public AlisDbInitializer(AlisDbContext alisDbContext)
    {
        if (!alisDbContext.Database.Exists())
        {
            Seed(alisDbContext);
        }          
    }

Upvotes: 0

J.W.
J.W.

Reputation: 18181

You need call Database.SetInitializer(new SiteDBInitializer()); instead.

Upvotes: 8

Related Questions