3

If I add a property to a class or add an entire class to the DbSet in my database file, Entity Framework should automatically add tables columns accordingly. This is working fine, but my issue is dropping the entire database and recreating it.

The remote server I am using doesn't allow me to recreate the database through Visual Studio and I must go and make an entire new database manually if I add/change classes/properties. Also, it won't let me delete the reference to the database even though it technically has been deleted so I have to rename the new database to another name.

Is there any way that I can change the structure of my database easily without dropping and recreating it?

2 Answers 2

4

Yes, there is a way. Use Entity Framework migrations. Start from executing this from your package manager console:

Enable-Migrations

Here is an article on MSDN about it. You have an option of using migrations only if you use Entity Framework Code First approach.

The cool thing about EF migrations is that you have automatic migrations option that you can enable. Basically all your schema changes that would not affect data integrity or loss (e.g. adding columns or new tables) in your database can be automatically pushed without explicitly implementing migration.

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

2 Comments

Thanks for the help!
@user3330265 you're welcome. Good luck with migrations!
0

Requirements

Install Entity Framework to your Visual Studio Application

Steps

  1. Import The CodeFirst Model (You seems to already have your model in hand, just make sure it is a Code First Model.)

  2. Use the package manager Console in VS and run :

    Add-Migration -IgnoreChanges

  3. On your model object, make any desired Changes

  4. Redo the Add-Migration command without the -IgnoreChanges flag

    Add-Migration

  5. Update the Database

    Update-Database

Notes

You may add options and C# files to control your migration

Update-Database -ProjectName Model -StartUpProjectName Model -Verbose -ConfigurationTypeName SpecialConfigurationForThisMigration

SpecialConfigurationForThisMigration.cs

internal sealed class Configuration : DbMigrationsConfiguration<Model.Model>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            MigrationsDirectory = @"Customer\\Migrations";
        }

        protected override void Seed(Model.Model context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.

        }
    }

Comments

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.