1

How can I use SetInitializer to only update the database? I don't want to drop and recreate it, because I've added only a column:

Database.SetInitializer<myDbContext>( /* Here */ );
3
  • 2
    You should to add migrations to it! Commented May 7, 2021 at 13:35
  • 1
    @cura I'm using EF5 Commented May 7, 2021 at 13:36
  • 1
    @user1187282 EF5 can still use migrations. Commented May 7, 2021 at 14:11

1 Answer 1

2

Basically you need to use DropCreateDatabaseIfModelChanges since your model classes (entity classes) have been changed.

You can also create your own custom initializer, if the above do not satisfy your requirements or you want to do some other process that initializes the database using the above initializer.

That should be something like this:

public class YourCustomInitializer : DropCreateDatabaseIfModelChanges<YourDBContext>
{
    protected override void Seed(YourDBContext context)
    {
        // sample data to be written or updated to the DB
    }
}

It is worth mentioning, you can simply add your new column to your entity, then use add-migration and update-database commands. You need to run these commands in the PackageManagerConsole.

Sign up to request clarification or add additional context in comments.

3 Comments

how can i add a column using the DbContext ?
@user1187282 Take a look at my updated answer.
Thought OP didn't want to drop and create database?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.