seed method not called with EntityFramework CodeFirst

16,038

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

Share:
16,038
Peter Kellner
Author by

Peter Kellner

Peter Kellner React, .Net, Ext Training Videos PeterKellner.Net

Updated on June 05, 2022

Comments

  • Peter Kellner
    Peter Kellner almost 2 years

    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();
        }
    }
    
    }
    
  • Peter Kellner
    Peter Kellner about 12 years
    I assume you mean: Database.SetInitializer(new SiteDBInitializer()); I tried that and it did not work either.
  • Eranga
    Eranga about 12 years
    @PeterKellner It gets called when you do something(eg Querying) for the first time. Did you try executing a query?
  • Peter Kellner
    Peter Kellner about 12 years
    Hi Eranga and J.W. I've updated my question to include a trivial console app that I believe seed should be called and it does not. If you could take a look (and even paste it in and run it), I'd really appreciate it. I'm sure there is something simple I'm missing but it has been driving be crazy for months.
  • Eranga
    Eranga about 12 years
    @PeterKellner Your example does not query the database. Replace var x = db.Customers; with var x = db.Customers.ToList();
  • Peter Kellner
    Peter Kellner about 12 years
    Thanks, you have it exactly right Eranga, that did fix my problem. It seems that when I call "Database.SetInitializer(new SiteDBInitializer());", my database gets recreated every time. How can I write the initializer so that the first time it creates the database if it does not exist and seed it, then after that it does not recreate the database? I've been toying with DropCreateDatabaseIfModelChanges, but that does not seem to create the database the first time.
  • Peter Kellner
    Peter Kellner about 12 years
    I finally got tired of not really understanding how all this works so I spent several hours writing three blog posts this morning that explains it all. Thanks for everyones help. peterkellner.net/2012/02/17/…