1

I have created my mvc 4 application using code first and accordingly database and table also generated now i want to delete one column (from backend) of my table. so i just want to know is there any way so that changes can occur in my code automatically according to change in database.

2 Answers 2

1

through package manager console using migration technique

PM> enable-migrations -EnableAutomaticMigrations

in code configuration do the following

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}

now when model changes do the following.

PM> update-database

Doing it through code

Use DropCreateDatabaseAlways initializer for your database. It will always recreate database during first usage of context in app domain:

Database.SetInitializer(new DropCreateDatabaseAlways<YourContextName>());

Actually if you want to seed your database, then create your own initializer, which will be inherited from DropCreateDatabaseAlways:

public class MyInitializer : DropCreateDatabaseAlways<YourContextName>
{
     protected override void Seed(MagnateContext context)
     {
         // seed database here
     }
}

And set it before first usage of context

Database.SetInitializer(new MyInitializer());
Sign up to request clarification or add additional context in comments.

2 Comments

but i do not want to change model manually what i want that model or other resources of code should get change automatically when i do change in database through back end. is there any way ? i do not know so want to confirm. (Thanks to reply)
@user3698892 hmmm no this is not possible even in database first approach you have to refresh/update model when back end database changes.
0

Well if you are using code first technique then remove column from your model and run migration script(google it) this will remove column from your database. But what you want is reverse which I am not sure could be done or not.

1 Comment

Even i dont know so trying to confirm anyway thanks for your suggestion.

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.