none
Seed Data not populating table RRS feed

  • Question

  • I have created a SeedData class, and populated it with the values that I would like to be populated into the table. When I run the application, I am not getting any errors, but the table is not populating with the Seed Data.

    I am using ASP.NET MVC3 and EF 4.1

    I have tried both DropCreateDatabaseAlways and DropCreateIfModelChanges with no luck

    System.Data.Entity.Database.SetInitializer(new EDWv2.Models.SeedData());
    


    My SeedData class is define as follows:

    public class SeedData : DropCreateDatabaseAlways<EDWContext>
    {
        protected override void Seed(EDWContext context)
        {
    
            var controlsystemdesignation = new List<ControlSystemDesignation>
            {
                new ControlSystemDesignation { Name = "BA"},
                new ControlSystemDesignation { Name = "BP"},
                new ControlSystemDesignation { Name = "RA"},
                new ControlSystemDesignation { Name = "GA"},
                new ControlSystemDesignation { Name = "WA"},
                new ControlSystemDesignation { Name = "WB"},
                new ControlSystemDesignation { Name = "WC"},
                new ControlSystemDesignation { Name = "EA"},
                new ControlSystemDesignation { Name = "EB"},
                new ControlSystemDesignation { Name = "EC"},
                new ControlSystemDesignation { Name = "DA"},
                new ControlSystemDesignation { Name = "DB"},
                new ControlSystemDesignation { Name = "DC"},
                new ControlSystemDesignation { Name = "FA"},
                new ControlSystemDesignation { Name = "FB"},
                new ControlSystemDesignation { Name = "SA"},
                new ControlSystemDesignation { Name = "SB"},
                new ControlSystemDesignation { Name = "SC"},
                new ControlSystemDesignation { Name = "LA"},
                new ControlSystemDesignation { Name = "LD"},
                new ControlSystemDesignation { Name = "DP"},
                new ControlSystemDesignation { Name = "PA"},
                new ControlSystemDesignation { Name = "PB"},
                new ControlSystemDesignation { Name = "PC"},
                new ControlSystemDesignation { Name = "UL"},
                new ControlSystemDesignation { Name = "UA"}
        };
            base.Seed(context);
            context.SaveChanges();
        }
    }
    


    Thanks,

    Jason


    jason
    Monday, January 30, 2012 8:42 PM

Answers

  • Hi Jason;

    You need to add each entity to the context as the ForEach method at the end does and do not do a SaveChanges at the end, Entity Framework will take care of that when ready.

    public class SeedData : DropCreateDatabaseAlways<EDWContext>
    {
        protected override void Seed(EDWContext context)
        {
    
            new List<ControlSystemDesignation>
            {
                new ControlSystemDesignation { Name = "BA"},
                new ControlSystemDesignation { Name = "BP"},
                new ControlSystemDesignation { Name = "RA"},
                new ControlSystemDesignation { Name = "GA"},
                new ControlSystemDesignation { Name = "WA"},
                new ControlSystemDesignation { Name = "WB"},
                new ControlSystemDesignation { Name = "WC"},
                new ControlSystemDesignation { Name = "EA"},
                new ControlSystemDesignation { Name = "EB"},
                new ControlSystemDesignation { Name = "EC"},
                new ControlSystemDesignation { Name = "DA"},
                new ControlSystemDesignation { Name = "DB"},
                new ControlSystemDesignation { Name = "DC"},
                new ControlSystemDesignation { Name = "FA"},
                new ControlSystemDesignation { Name = "FB"},
                new ControlSystemDesignation { Name = "SA"},
                new ControlSystemDesignation { Name = "SB"},
                new ControlSystemDesignation { Name = "SC"},
                new ControlSystemDesignation { Name = "LA"},
                new ControlSystemDesignation { Name = "LD"},
                new ControlSystemDesignation { Name = "DP"},
                new ControlSystemDesignation { Name = "PA"},
                new ControlSystemDesignation { Name = "PB"},
                new ControlSystemDesignation { Name = "PC"},
                new ControlSystemDesignation { Name = "UL"},
                new ControlSystemDesignation { Name = "UA"}
            }.ForEach( c => context.Brands.Add( c ) ); 
            
            base.Seed( context );
        }
    }
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, January 30, 2012 9:41 PM

All replies

  • Hi Jason;

    You need to add each entity to the context as the ForEach method at the end does and do not do a SaveChanges at the end, Entity Framework will take care of that when ready.

    public class SeedData : DropCreateDatabaseAlways<EDWContext>
    {
        protected override void Seed(EDWContext context)
        {
    
            new List<ControlSystemDesignation>
            {
                new ControlSystemDesignation { Name = "BA"},
                new ControlSystemDesignation { Name = "BP"},
                new ControlSystemDesignation { Name = "RA"},
                new ControlSystemDesignation { Name = "GA"},
                new ControlSystemDesignation { Name = "WA"},
                new ControlSystemDesignation { Name = "WB"},
                new ControlSystemDesignation { Name = "WC"},
                new ControlSystemDesignation { Name = "EA"},
                new ControlSystemDesignation { Name = "EB"},
                new ControlSystemDesignation { Name = "EC"},
                new ControlSystemDesignation { Name = "DA"},
                new ControlSystemDesignation { Name = "DB"},
                new ControlSystemDesignation { Name = "DC"},
                new ControlSystemDesignation { Name = "FA"},
                new ControlSystemDesignation { Name = "FB"},
                new ControlSystemDesignation { Name = "SA"},
                new ControlSystemDesignation { Name = "SB"},
                new ControlSystemDesignation { Name = "SC"},
                new ControlSystemDesignation { Name = "LA"},
                new ControlSystemDesignation { Name = "LD"},
                new ControlSystemDesignation { Name = "DP"},
                new ControlSystemDesignation { Name = "PA"},
                new ControlSystemDesignation { Name = "PB"},
                new ControlSystemDesignation { Name = "PC"},
                new ControlSystemDesignation { Name = "UL"},
                new ControlSystemDesignation { Name = "UA"}
            }.ForEach( c => context.Brands.Add( c ) ); 
            
            base.Seed( context );
        }
    }
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, January 30, 2012 9:41 PM
  • thank you for the help, this helped me get the data to seed into the database

     

    Jason


    jason
    Tuesday, January 31, 2012 7:51 PM
  •  

    Not a problem Jason, glad I was able to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, January 31, 2012 8:03 PM