locked
Entity Framework 4.1 ASP.NET MVC3 Seeding database does not work RRS feed

  • Question

  • I have followed an ASP.NET MVC 3  tutorial and successfully built a website built on an SQL database created using Entity Framework Model First methods.

     

    I want to seed the database with default data but I get an exception thrown.

     

    1)      I added an initialise class to the DAL namespace

      public class MyModelInitialise : DropCreateDatabaseIfModelChanges<MyModelContext>

        {

            protected override void Seed(MyModelContext context)

            {

                base.Seed(context);

     

                var BenchMarks = new List<BenchMark>

                {

                 new BenchMark{ Name="BenckMark1", Value="250"},

                 new BenchMark{ Name="BenckMark2", Value="350"},

                 new BenchMark{ Name="BenckMark3", Value="450"}

                };

                BenchMarks.ForEach(bm => context.BenchMarks.Add(bm));

     

                context.SaveChanges();

            }

        }

     

    2)  I added the initialiser to app startup

      protected void Application_Start()

           {

                Database.SetInitializer<MyModelContext>(new MyModelInitialise());

    }

     

    When I invoke a webpage I get the following exception:

    Model compatibility cannot be checked because the DbContext instance was not created using Code First patterns. DbContext instances created from an ObjectContext or using an EDMX file cannot be checked for compatibility.

    Any ideas please?

    Wednesday, May 25, 2011 4:36 PM

Answers

  • Hi,

    The built in database initializers are designed around creating a database and can only really be used with Code First. You can write your own initializer that inherits from IDatabaseInitializer<TContext> and just takes care of seeding.

    public class Test : IDatabaseInitializer<MyModelContext>
    {
      public void InitializeDatabase(MyModelContext context)
      {
        // Seed logic here
      }
    }

    ~Rowan

    • Proposed as answer by Rowan Miller Wednesday, May 25, 2011 8:03 PM
    • Marked as answer by Alan_chen Thursday, June 2, 2011 7:33 AM
    Wednesday, May 25, 2011 8:03 PM

All replies

  • Hi,

    The built in database initializers are designed around creating a database and can only really be used with Code First. You can write your own initializer that inherits from IDatabaseInitializer<TContext> and just takes care of seeding.

    public class Test : IDatabaseInitializer<MyModelContext>
    {
      public void InitializeDatabase(MyModelContext context)
      {
        // Seed logic here
      }
    }

    ~Rowan

    • Proposed as answer by Rowan Miller Wednesday, May 25, 2011 8:03 PM
    • Marked as answer by Alan_chen Thursday, June 2, 2011 7:33 AM
    Wednesday, May 25, 2011 8:03 PM
  • Hi NBuckland,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 31, 2011 6:54 AM
  • This was exactly what I needed, thanks.

    So in my app for example, my global.asax looks like this..

            protected void Application_Start()
            {
    
                // my IDatabaseInitializer, seeding only version
                Database.SetInitializer(new MusicStoreDbEntitiesInitializer());
    
                AreaRegistration.RegisterAllAreas();
    
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }

    And the initializer class it is calling looks like this...

    public class MusicStoreDbEntitiesInitializer : IDatabaseInitializer<MusicStoreDBEntities>
    {
        public void InitializeDatabase(MusicStoreDBEntities context)
        {
            context.Artists.Add(new Artist { Name = "Al Di Meola" });
            context.Genres.Add(new Genre { Name = "Jazz" });
            context.Albums.Add(new Album
                                {
                                    Artist = new Artist { Name = "Rush" },
                                    Genre = new Genre { Name = "Rock" },
                                    Price = 9.99m,
                                    Title = "Caravan"
                                });
    
            context.SaveChanges();
        }
    }

    Hope this helps someone else.

    Sunday, July 29, 2012 11:36 PM