简体   繁体   中英

Add column to table in C# & Entity Framework code-first without deleting DB content

I have a database with several tables in AZURE SQL Server.

I want to add a new column to one of the tables, but I want to keep all the content of the tables including the table with the new column; hence I don't want to use the

Database.SetInitializer<MyDbContext>(new DropCreateDatabaseIfModelChanges<MyDbContext>());

My code looks like this:

public class DBTables : DbContext
{
    public DBTables() : base(ProductKeys.GetDbKey()) 
    {
    }

    public DbSet<ElementsForPointTable> ElementsForPointTable { get; set; }
    public DbSet<StationsDetailsTable> StationsDetailsTable { get; set; }
}

How do I do it?

You could use migrations to update the database scheme.

  1. Open Package Manager Console (Tools -> Library Package Manager -> Package Manager Console)
  2. Run Enable-Migrations command
  3. Change your model
  4. Run Add-Migration command in Package Manager Console.
  5. Run Update-Database command to apply your model changes to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM