0
votes

In the application I'm building the very first thing the client does is request the breeze metadata. If the database does not exist and entity framework needs to create the database it will not seed using the initializer configured with Database.SetInitializer.

If something else triggers EF to do a query against the dbcontext first then the database is seeded as expected.

I'm not sure if this is a bug or intended for some reason?

Thanks

1

1 Answers

2
votes

I'm pretty sure the bug is on your end. I have no trouble retrieving metadata first at which point the database is created and seeded. Do it all the time.

You can see this for yourself in any of the Breeze Todo samples. Remember that your first Breeze client query always asks for metadata before processing your query so the first call to the server (in these samples) is always a request for metadata. These samples would not work if a request for metadata failed to generate the database.

The Todos sample initializes and seeds the database in a trivial way in the class ctor (the static ctor). Here's the entire TodosContext.cs

namespace Todo.Models {
    using System.Data.Entity;

    public class TodosContext : DbContext 
    {
        // DEVELOPMENT ONLY: initialize the database
        static TodosContext()
        {
            Database.SetInitializer(new TodoDatabaseInitializer());
        }
        public DbSet<TodoItem> Todos { get; set; }
    }
}

To see it in action:

  1. Show all files

  2. Delete the *App_Data/todo.sdf* database

  3. Set breakpoints on that constructor and on the methods of the Web API controller.

  4. Run with debug (F5) ... you'll see the metadata endpoint hit first, then this static constructor.

  5. Look at the *App_Data* folder in Windows Explorer and confirm that the database was created.

  6. Continue ... you'll see the Todos query endpoint hit.

  7. Continue ... the screen fills with seeded todos.

How are you doing it?