Entity framework, POCO, ¿cómo informar el progreso al usuario durante la inicialización de la base de datos?

I am trying to figure out a way so I can report the progress on initialize DB schema stage to user though a progress bar.

The problem is: I don't know where to call the 'ReportProgress' function? I am currently overwrite 'Seed' function to populate initial DB data, could I overwrite Database 'Initialize' function so the progress could be updated after the creation of each table?

EDIT: Según este artículo: Creating a Code First Database Initializer Strategy

Implementación de IDatabaseInitializer y su función InitializeDatabase and put my own database and table creating code inside should do what I want. Then instead of inside Seed function, I could raise events inside InitializeDatabase. That should working, but I have yet reach a point that I need go though all these trouble to show progress of DB initialize to end user, now a waiting cursor works for me.

preguntado el 01 de febrero de 12 a las 04:02

Do you manually create tables inside the Seed ¿método? -

No, the 'Seed' only used to generate data. Ok if I do that I can update progress inside 'Seed' method? -

1 Respuestas

EF initializers do not expose the progress through any events.

Declare some events that you want to raise when some state changes in the seed method.

public class MyInitializer : CreateDatabaseIfNotExists<MyContext>
{
    public event EventHandler<SeedStateChangeEventArgs> SeedStateChange;

    protected virtual void Seed(MyContext context)
    {
       // create a table
       OnSeedStateChange(new SeedStateChangeEventArgs { Table = "Foo" });

       // create another table
       OnSeedStateChange(new SeedStateChangeEventArgs { Table = "Bar" });
    }

    protected virtual void OnSeedStateChange(SeedStateChangeEventArgs args)
    {
         // raise event here
    }
}

Entonces en tu Form

var initializer = new MyInitializer();
initializer.SeedStateChange += MyStateChangeHandler;
Database.SetInitializer(initializer);

var context = new MyContext();
context.Database.Initialize(false);

This is just an outline of the concept. I haven't tried this

Respondido 01 Feb 12, 08:02

Hi, according to this document for 'Seed' method': msdn.microsoft.com/en-us/library/gg679221%28v=vs.103%29.aspx - "When overridden adds data to the context for seeding. The default implementation does nothing.", doesn't looks like using 'Seed' in that way will work, as DB structure should already been created before calling 'Seed'. - Paul L

@pstar call base.Seed() inside your overridden method - Eranga

I put base.Seed(context) on first line of my override Seed Method, but the DB structure already been created at this point when I do a debug/break. I think I need overwrite InitializeDatabase or OnModelCreating método. - Paul L

happy with the idea you provided, but I do think I need use InitializeDatabase en lugar de Seed método - Paul L

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.