CloudyKooper
CloudyKooper

Reputation: 717

How can I prevent my database from being recreated without seeding?

I would like to prevent my program from dropping the database each time I run the debugger during the developing stage. I want to do this without the usual seeding the database. Since I will be using the Import and Export Data wizard, I would like to use this method of populating my database.

Is there a method to prevent the program from dropping the database?

Here's more information that I hope will help:

My Initializer

DropCreateDatabaseIfModelChanges<SchoolInDB>
{

    protected override void Seed(SchoolInDB context)
    {

        context.Parents.Add(new Parent { Name = "Mary Lawson", Phone = "949-999-9999", Notes = "Please see IEP " });


        base.Seed(context);


    }

}

My application start

protected void Application_Start()
    {
     System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseAlways<SchoolIn.Models.SchoolInDB>()); 

        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

My DBContext

public class SchoolInDB : DbContext
{
    public DbSet<Course> Courses { get; set; }
    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<Parent> Parents { get; set; }
    public DbSet<PdfReport> PdfReports { get; set; }
    public DbSet<CourseProgress> CourseProgresses { get; set; }
    public DbSet<ParentContact> ParentContacts { get; set; }
    public DbSet<RedAlert> RedAlerts { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<Assignment> Assignments { get; set; }


}

My connection string

<connectionStrings>
<add name="ApplicationServices"
    connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
    providerName="System.Data.SqlClient" />

I guess I want to avoid using the SetInitializer to DropCreateDatabaseAlways and just load the database whether the models changes or not. I'll be using the Import and Export Wizard to populate the database.

Thanks for helping!

Upvotes: 2

Views: 2575

Answers (2)

clayRay
clayRay

Reputation: 743

To elaborate on RickAnd's answer, for future searchers looking for an answer.

I assume your full initializer class declaration is ..

public class ApplicationDbInitializer : DropCreateDatabaseAlways<SchoolInDB> 

You simply need to replace this with...

public class ApplicationDbInitializer : CreateDatabaseIfNotExists<SchoolInDB>

That way your database will still be initialised with your seed values, but won't be dropped each time you start the application.

Upvotes: 1

RickAndMSFT
RickAndMSFT

Reputation: 22770

I guess I want to avoid using the SetInitializer to DropCreateDatabaseAlways and just load the database whether the models changes or not. I'll be using the Import and Export Wizard to populate the database

So don't call SetInitializer. Just use the connection string to open the DB. BTW, DropCreateDatabaseIfModelChanges as the name implies, only drops/creates the DB when the schema changes.

That's how traditional DB first works.

Upvotes: 1

Related Questions