简体   繁体   English

将列添加到 C# 和实体框架代码中的表中,而不删除数据库内容

[英]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.我在 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运行Enable-Migrations命令
  3. Change your model改变你的模型
  4. Run Add-Migration command in Package Manager Console.在包管理器控制台中运行Add-Migration命令。
  5. Run Update-Database command to apply your model changes to the database.运行Update-Database命令以将您的模型更改应用于数据库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#交互式窗口中的实体框架Code-First(Oracle) - Entity framework Code-First in c# interactive window (Oracle) C#-实体框架-大种子数据代码优先 - C# - Entity Framework - Large seed data code-first 是否可以在不使用Entity Framework中以代码优先方式使用C#属性的情况下生成数据库字段? - Is it possible to generate database fields without using C# properties in Entity Framework, code-first approach? SQL Server表设计建议(Entity Framework代码优先,C#) - SQL Server table design advice (Entity Framework code-first, C#) 为什么实体框架代码优先(使用现有数据库)继续尝试从EdmMetadata表获取数据? - Why does Entity Framework Code-First (with an existing DB) keep trying get data from an EdmMetadata table? Entity Framework Core 2.1,使用代码优先迁移重命名表而不删除和创建新表 - Entity Framework Core 2.1, rename table using code-first migrations without dropping and creating new table 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? SqlQuery 中无法 select 特定列 - 实体框架代码优先 - Cannot select specific column in SqlQuery - Entity Framework code-first 在实体框架代码优先迁移中选择列名 - Choosing name of column in entity framework code-first migrations C#实体框架与该类中相同类的代码优先关系 - C# Entity Framework code-first relationship to same class within the class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM