locked
Seeding db code not working. RRS feed

  • Question

  • Edit: I found the problem,  Turns out the data is getting put into the db, but when I do foreach loops, I am not loading the realated entities.  It helps to look in the tables once and a while :)

     

    I have a model that has 4 entities, all one to many relationships. I am trying to seed the db but only the top level Issuer is getting added to the db.

    Here is my model.

    public class MuniData : DbContext
    {
    	public DbSet<Issuer> Issuers { get; set; }
    	public DbSet<Report> Reports { get; set; }
    	public DbSet<Bond> Bonds { get; set; }
    	public DbSet<Maturity> Maturities { get; set; }
    
    	public MuniData()
    	{
    		Database.SetInitializer(new DBInitializer());
    	}
    }
    public class Issuer
    {
    	public int ID { get; set; }
    	public string IssuerName { get; set; }
    	public virtual ICollection<Report> Reports { get; set; }
    
    	public Issuer(){this.Reports = new List<Report>();}
    }
    public class Report
    {
    	public int ID { get; set; }
    	public string ReportName { get; set; }
    	public virtual ICollection<Bond> Bonds { get; set; }
    
    	public Report(){this.Bonds = new List<Bond>();}
    
    }
    public class Bond
    {
    	public int ID { get; set; }
    	public string BondTitle { get; set; }
    	public virtual ICollection<Maturity> Maturities { get; set; }
    
    	public Bond(){this.Maturities = new List<Maturity>();}
    }
    public class Maturity
    {
    	public int ID { get; set; }
    	public int MatAmount { get; set; }
    }
    

    Here is the SetInitializer code with the complex insert code.

     

    protected override void Seed(MuniData db)
    {
    Issuer issuer = new Issuer
    {
    IssuerName = "City of Austin",
    Reports = new[] 
    {
    new Report
    {
    ReportName = "Go Report",
    Bonds = new[]
    {
    new Bond
    {
    BondTitle = "Series 2011",
    Maturities = new[]
    {
    new Maturity{MatAmount = 100000, Test = "tps"},
    new Maturity{MatAmount = 200000, Test = "tps"},
    new Maturity{MatAmount = 300000, Test = "tps"},
    new Maturity{MatAmount = 400000, Test = "tps"},
    }
    },
    new Bond
    {
    BondTitle = "Series 2012",
    Maturities = new[]
    {
    new Maturity{MatAmount = 500000, Test = "tps"},
    new Maturity{MatAmount = 600000, Test = "tps"},
    new Maturity{MatAmount = 700000, Test = "tps"},
    new Maturity{MatAmount = 800000, Test = "tps"},
    }
    }
    }
    },
    new Report
    {
    ReportName = "Rev Report",
    Bonds = new[]
    {
    new Bond
    {
    BondTitle = "Series 2013",
    Maturities = new[]
    {
    new Maturity{MatAmount = 100000, Test = "tps"},
    new Maturity{MatAmount = 200000, Test = "tps"},
    new Maturity{MatAmount = 300000, Test = "tps"},
    new Maturity{MatAmount = 400000, Test = "tps"},
    }
    },
    new Bond
    {
    BondTitle = "Series 2014",
    Maturities = new[]
    {
    new Maturity{MatAmount = 500000, Test = "tps"},
    new Maturity{MatAmount = 600000, Test = "tps"},
    new Maturity{MatAmount = 700000, Test = "tps"},
    new Maturity{MatAmount = 800000, Test = "tps"},
    }
    }
    }
    }
    }
    };
    
    db.Issuers.Add(issuer);
    }
    
    
    What happens when I iterate thought the db I get the top level Issuer, but none of the chained entities are in the db.

     


    Thanks, Terrence
    • Edited by Terrence_ Thursday, December 29, 2011 6:14 PM
    Thursday, December 29, 2011 5:20 PM

Answers

  • Hi Terrence_;

    This line of code need to be executed before anything is done with the database, place it in program initialization or before creating the DbContext. Also to be able to seed the data into the database you need to create you own initializers that inherits from one of the three initializers. In the below statement I am using an initializer inherited from DropCreateDatabaseIfModelChanges.

    Database.SetInitializer( new DropCreateDatabaseMuniDataIfModelChangesWithSeed() );

    This is the initializer class

    public class DropCreateDatabaseMuniDataIfModelChangesWithSeed : DropCreateDatabaseIfModelChanges<MuniData>
    {
        protected override void Seed( MuniData context )
        {
            {
                context.Issuers.Add( new Issuer
                {
                    IssuerName = "City of Austin",
                    Reports = new[ ] 
                    { new Report
                        {
                            ReportName = "Go Report",
                            Bonds = new[]
                            {
                                new Bond
                                {
                                    BondTitle = "Series 2011",
                                    Maturities = new[]
                                    {
                                        new Maturity{MatAmount = 100000},
                                        new Maturity{MatAmount = 200000},
                                        new Maturity{MatAmount = 300000},
                                        new Maturity{MatAmount = 400000},
                                    }
                                },
                                new Bond
                                {
                                    BondTitle = "Series 2012",
                                    Maturities = new[]
                                    {
                                        new Maturity{MatAmount = 500000},
                                        new Maturity{MatAmount = 600000},
                                        new Maturity{MatAmount = 700000},
                                        new Maturity{MatAmount = 800000},
                                    }
                                }
                            }
                        },
                        new Report
                        {
                            ReportName = "Rev Report",
                            Bonds = new[]
                            {
                                new Bond
                                {
                                    BondTitle = "Series 2013",
                                    Maturities = new[]
                                    {
                                        new Maturity{MatAmount = 100000},
                                        new Maturity{MatAmount = 200000},
                                        new Maturity{MatAmount = 300000},
                                        new Maturity{MatAmount = 400000},
                                    }
                                },
                                new Bond
                                {
                                    BondTitle = "Series 2014",
                                    Maturities = new[]
                                    {
                                        new Maturity{MatAmount = 500000},
                                        new Maturity{MatAmount = 600000},
                                        new Maturity{MatAmount = 700000},
                                        new Maturity{MatAmount = 800000},
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }
    }
    

    In the above code I removed the field, Test = "tps" , from lines like this :

    new Maturity{MatAmount = 100000, Test = "tps"},

    Because in the class Maturity you had no such field.

    Now all you need to do is instantiate the context and query the database and all should work.

     

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Terrence_ Thursday, December 29, 2011 11:33 PM
    Thursday, December 29, 2011 7:53 PM

All replies

  • Hi Terrence_;

    This line of code need to be executed before anything is done with the database, place it in program initialization or before creating the DbContext. Also to be able to seed the data into the database you need to create you own initializers that inherits from one of the three initializers. In the below statement I am using an initializer inherited from DropCreateDatabaseIfModelChanges.

    Database.SetInitializer( new DropCreateDatabaseMuniDataIfModelChangesWithSeed() );

    This is the initializer class

    public class DropCreateDatabaseMuniDataIfModelChangesWithSeed : DropCreateDatabaseIfModelChanges<MuniData>
    {
        protected override void Seed( MuniData context )
        {
            {
                context.Issuers.Add( new Issuer
                {
                    IssuerName = "City of Austin",
                    Reports = new[ ] 
                    { new Report
                        {
                            ReportName = "Go Report",
                            Bonds = new[]
                            {
                                new Bond
                                {
                                    BondTitle = "Series 2011",
                                    Maturities = new[]
                                    {
                                        new Maturity{MatAmount = 100000},
                                        new Maturity{MatAmount = 200000},
                                        new Maturity{MatAmount = 300000},
                                        new Maturity{MatAmount = 400000},
                                    }
                                },
                                new Bond
                                {
                                    BondTitle = "Series 2012",
                                    Maturities = new[]
                                    {
                                        new Maturity{MatAmount = 500000},
                                        new Maturity{MatAmount = 600000},
                                        new Maturity{MatAmount = 700000},
                                        new Maturity{MatAmount = 800000},
                                    }
                                }
                            }
                        },
                        new Report
                        {
                            ReportName = "Rev Report",
                            Bonds = new[]
                            {
                                new Bond
                                {
                                    BondTitle = "Series 2013",
                                    Maturities = new[]
                                    {
                                        new Maturity{MatAmount = 100000},
                                        new Maturity{MatAmount = 200000},
                                        new Maturity{MatAmount = 300000},
                                        new Maturity{MatAmount = 400000},
                                    }
                                },
                                new Bond
                                {
                                    BondTitle = "Series 2014",
                                    Maturities = new[]
                                    {
                                        new Maturity{MatAmount = 500000},
                                        new Maturity{MatAmount = 600000},
                                        new Maturity{MatAmount = 700000},
                                        new Maturity{MatAmount = 800000},
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }
    }
    

    In the above code I removed the field, Test = "tps" , from lines like this :

    new Maturity{MatAmount = 100000, Test = "tps"},

    Because in the class Maturity you had no such field.

    Now all you need to do is instantiate the context and query the database and all should work.

     

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Terrence_ Thursday, December 29, 2011 11:33 PM
    Thursday, December 29, 2011 7:53 PM
  • Thanks Fernando, I figured my error out and put the following Edit at the top of my post.

    Edit: I found the problem, Turns out the data is getting put into the db, but when I do foreach loops, I am not loading the realated entities. It helps to look in the tables once and a while :)

     


    Thanks, Terrence
    Thursday, December 29, 2011 11:33 PM
  •  

    Not a problem Terrence, always glad to help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, December 30, 2011 2:04 AM