locked
Trouble getting seed to be called RRS feed

  • Question

  • I've been struggling trying to get Seed to be called.  In my below example, it never does.  Any suggestions?

    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());
                Database.SetInitializer(new DropCreateDatabaseAlways<SiteDB>());
                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 http://peterkellner.net Microsoft MVP • ASPInsider

    Friday, February 17, 2012 5:11 AM

Answers

  • Hi Peter;

    The two issues I see are as follows: first the initial Database.SetInitializer command you used is correct. You should be using this one.

    Database.SetInitializer( new SiteDBInitializer( ) );

    The second issue is that this statement :

    using( var db = new SiteDB( ) )
    {
        var x = db.Customers;
    }

    Is not causing EF to execute a command on the server. Use something that will return something like the following:

    using( var db = new SiteDB( ) )
    {
        var x = db.Customers.ToList();
    }

    .

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Peter Kellner Friday, February 17, 2012 9:36 PM
    Friday, February 17, 2012 5:54 AM

All replies

  • Hi Peter;

    The two issues I see are as follows: first the initial Database.SetInitializer command you used is correct. You should be using this one.

    Database.SetInitializer( new SiteDBInitializer( ) );

    The second issue is that this statement :

    using( var db = new SiteDB( ) )
    {
        var x = db.Customers;
    }

    Is not causing EF to execute a command on the server. Use something that will return something like the following:

    using( var db = new SiteDB( ) )
    {
        var x = db.Customers.ToList();
    }

    .

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Peter Kellner Friday, February 17, 2012 9:36 PM
    Friday, February 17, 2012 5:54 AM
  • Thanks Fernando. that was it.  simple error but I had other errors in my thinking that caused me to think it was other things.

    I just wrote a 3 part blog posts about this and the new Migration stuff.  I think I now finally understand it.   Phew.

    http://peterkellner.net/2012/02/17/entityframework-4-3-codefirst-trivial-one-file-example-part-1/


    Peter Kellner http://peterkellner.net Microsoft MVP • ASPInsider

    Friday, February 17, 2012 9:37 PM
  •  

    Not a problem Peter, glad I was able to help. Great job on the post.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Saturday, February 18, 2012 2:09 PM