How to debug code insde the overriden Seed Method (inside Database Initializer) RRS feed

  • Question

  • In my Silverlight solution that uses EF Code First, I follow the suggest to generate my sample data for the solution as below:

    1) In my DbContext implementation constructor, I invoke Database.SetInitializer(), and pass a custom class (LoadSampleDataInitializer) that generates the sample data.

    like this...

    public partial class AdvisorDbContext : DbContext { public AdvisorDbContext() { this.Configuration.ValidateOnSaveEnabled = true; Database.SetInitializer(new LoadSampleDataInitializer()); }


    2) My LoadSampleDataInitializer inherits from DropCreateDatabaseAlways() and my sample data is created inside the overriden the Seed() method, like this...

     public class LoadSampleDataInitializer : DropCreateDatabaseAlways<AdvisorDbContext>
            protected override void Seed(AdvisorDbContext ctx)
     ... so on     

    This is all works just fine as long as there are no runtime errors in the code that generates the sample data... 

    If, however, the code inside the Seed() method has any runtime error , the solution seems to fails to build with the very unhelpful "Failed to get the MetadataWorkspace for the DbContext type..." .   That would seem to make sense if we accept the fact that EF Code First is actually executing this code at build time and can't proceed.    But what is conusing, is how then am I supposed to set breakpoints to step through my initialization code to see what the issue is?  (Note: in my I am reading and parsing a local XML file).

    • Edited by DoWorkAync Monday, March 25, 2013 2:07 PM
    Monday, March 25, 2013 2:03 PM

All replies

  • Hi  DoWorkAync,

    Welcome to the MSDN forum.

    Could you please share the code snippet in Seed method?

    After research, I find many of this kind of error is related to WCF RIA service with Silverlight. Do you use that technology?

    Good day.

    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 27, 2013 7:20 AM
  • Hello Alexander,

    Thanks, for your follow up, yes sure, please find a fragment .  It is essentially code that instantiates object graphs with sample data and loads them into the database using EF DbContext.    What I've noticed, is that there are two distinct phases where the code is executed:

    1) VS seems to parse my code (and run?) at build time, a step possibly needed for generating RIA references, This part seems to run without a debugger attached
    2) VS studio seems to also re-build and run the project in debug mode for EF Code First to build database (debugger is attached in this case)

    I would be grateful for any pointers on any further documentation on exactly what happens here?


    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Diagnostics;
    using System.Linq;
    using System.Reflection;
    using Advisor.DomainModels.DbContexts;
    namespace Advisor.DomainModels.Initializers
        public class LoadSampleDataInitializer : DropCreateDatabaseAlways<AdvisorDbContext>
            protected override void Seed(AdvisorDbContext ctx)
                #region Add Currencies
                var cy = ctx.AssetTypes.Single(at => at.Name.Contains("Currency"));
                new List<Currency>
                    {   new Currency {LongName = "Euro", Symbol = "EUR", AssetType = cy},
                        new Currency {LongName = "US Dollars", Symbol = "USD", AssetType=cy},
                        new Currency {LongName = "Swiss Francs", Symbol = "CHF",AssetType = cy},
                        new Currency {LongName = "British Pounds", Symbol = "GBP", AssetType = cy},
                    }.ForEach(currency => ctx.Currencies.Add(currency));
                foreach (var cu in ctx.Currencies)
                    // Currencies settle (in transactions) in their own currency
                    cu.SettlementCurrencyId = cu.Id;
                    //use USD for entering curreny rates (AssetPrices)
                    cu.PricingCurrencyId = ctx.Currencies.Single(currency => currency.Symbol == "USD").Id;
                #region Add Bonds
                var fixedRateBond = ctx.AssetTypes.Single(at => at.Name.Contains("FixedRateBond"));
                new List<FixedRateBond>
                        new FixedRateBond { LongName="JP Morgan 8.125% -12/02/2017", AssetType = fixedRateBond},
                        new FixedRateBond { LongName="Goldman Sachs 5.25% -12/02/2017", AssetType = fixedRateBond},
                        new FixedRateBond { LongName="Shell Corp 7.0% -12/02/2017", AssetType = fixedRateBond}
                     }.ForEach(b => ctx.Assets.Add(b));
                var floatingRateBond = ctx.AssetTypes.Single(at => at.Name.Contains("FloatingRateBond"));
                new List<FloatingRateBond>
                        new FloatingRateBond { LongName="Dubai Investment House -12/02/2020", AssetType = floatingRateBond},
                        new FloatingRateBond { LongName="Barclays, FRN - 03/04/2023", AssetType = floatingRateBond},
                        new FloatingRateBond { LongName="Shell Corp FRN  -12/02/2030", AssetType = floatingRateBond}
                     }.ForEach(b => ctx.Assets.Add(b));

    Wednesday, March 27, 2013 9:28 PM