Setting the Database initializer should not be done from the RunWeb persistor with code first any more

May 9, 2013 at 2:16 AM
Hi Guys,
           The NO fresh install package has the Persistor property in the RunWeb class as
protected override IObjectPersistorInstaller Persistor
    {
        get
        {
            // Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); //For in-memory database
            // Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyDbContext>()); //Optional behaviour for CodeFirst
            var installer = new EntityPersistorInstaller();

            // installer.UsingEdmxContext("Model").AssociateTypes(AdventureWorksTypes); // for Model/Database First
            // installer.UsingCodeFirstContext(() => new MyDbContext()).AssociateTypes(CodeFirstTypes);  //For Code First

            return installer;
        }
    }
However your current recommendation is to Context objects defined in the Model project
Where you would set the Database initializer.


protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Initialisation
        //Use the Naked Objects > DbInitialiser template to add an initialiser, then reference thus:
        //Database.SetInitializer(new MyDbInitialiser()); 
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<UWASDbContext>());  //Optional behaviour for CodeFirst

        //Mappings
        //Use the Naked Objects > DbMapping template to create mapping classes & reference them thus:
        //modelBuilder.Configurations.Add(new EmployeeMapping());
        //modelBuilder.Configurations.Add(new CustomerMapping());
    }
It looks like you should remove the commented out code in the Persistor property.

Regards,
          Alistair
Coordinator
May 9, 2013 at 9:45 AM
The commented-out code is provided just as an aide memoir for things you might want to do - I find it helpful. But there are several different ways of setting your database configurations up which can all work.