【问题标题】:Code First Migration - Entity Framework - unable to add column to the table代码优先迁移 - 实体框架 - 无法将列添加到表中
【发布时间】:2015-03-19 04:54:49
【问题描述】:

这个问题已经被多次以不同的方式提出过。我已经浏览了所有答案并花费了大量时间。我似乎无法正常工作。

我一直在研究 asp.net mvc Entity Framework,SQL Server 应用程序。我已经有一个现有的数据库、表等,并且一切正常。我只需要在表中添加一个新列。

所以.. 我在模型类中添加了一个属性,以便可以将列添加到表中。但没有成功。

所以我按以下顺序执行步骤。

  1. 在我的模型类中添加该字段。

    [Required]
    public string EmailSubject{ get; set; }
    
  2. 然后我删除我的 asp.net mvc 项目中包含 Configuration.cs 类的文件夹 Migrations
  3. 然后在包管理器控制台中,我成功发出以下命令。

    Enable-Migrations -ContextTypeName AppointmentReminder.Data.ReminderDb -Force
    
  4. 然后我将属性设置为true如下

    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }
    
  5. 然后我在包管理器控制台中发出以下命令。

    Update-Database -Verbose -Force
    

这是我到数据库默认连接的连接字符串的样子

Data Source=(LocalDb)\v11.0;AttachDbFilename=C:\practice\AppointmentReminder4\AppointmentReminder4\App_Data\aspnet-AppointmentReminder4-20141202060615.mdf;Initial Catalog=aspnet-AppointmentReminder4-20141202060615;Integrated Security=True

但我无法在所需表中添加新列。


编辑 1

我在没有必需属性的情况下按如下方式更新了模型并执行了上述所有步骤,但我仍然无法将列添加到表中。

public string EmailBody { get; set; }

这是所有命令及其输出。

PM> Enable-Migrations -ContextTypeName AppointmentReminder.Data.ReminderDb -Force
Checking if the context targets an existing database...
Code First Migrations enabled for project AppointmentReminder4.
PM> Update-Database -Verbose -Force
Using StartUp project 'AppointmentReminder4'.
Using NuGet project 'AppointmentReminder4'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'aspnet-AppointmentReminder4-20141202060615' (DataSource: (LocalDb)\v11.0, Provider: System.Data.SqlClient, Origin: Configuration).
No pending explicit migrations.
Running Seed method.
PM> Update-Database -Verbose -Force
Using StartUp project 'AppointmentReminder4'.
Using NuGet project 'AppointmentReminder4'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'aspnet-AppointmentReminder4-20141202060615' (DataSource: (LocalDb)\v11.0, Provider: System.Data.SqlClient, Origin: Configuration).
No pending explicit migrations.
Running Seed method.
PM> 

编辑 2 终于解决了这个问题。我上面的所有步骤都是正确的。除了我正在编辑我的模型类,而不是实际绑定到 ReminderDb(EF 对象)的实际数据类。在我用属性更新了正确的类之后,每件事都成功了。感谢所有提供帮助的人!

【问题讨论】:

  • 虽然我已经回答了我认为的答案,但您可能应该显示您遇到的错误。

标签: c# asp.net asp.net-mvc entity-framework entity-framework-migrations


【解决方案1】:

由于该字段是必填项,因此您需要指定一个默认值。编辑您的迁移文件,找到添加列的行,并指定默认值:

public override void Up()
{    
    AddColumn("dbo.MyTable", "EmailSubject", c => c.String(nullable: false, defaultValue: ""));
}

供参考: Default value for Required fields in Entity Framework migrations?

编辑:根据您的编辑,您需要先创建迁移,然后再尝试更新。请参阅this example,了解您通常如何使用它。

但是,如果您尝试将 Code First 迁移应用到现有数据库,这篇文章可能会有所帮助:Code First Migrations with an existing database

【讨论】:

  • 删除所需属性后仍然没有更新
  • @dotnet-practitioner 你没有在任何地方做add-migration,所以它没有什么可申请的。请参阅上面的更新。
【解决方案2】:

您使用必需属性修饰了新列。如果该表已经有一些行,则这些行不能有该列。 删除所需。与迁移相比,用数据填充所有行。 将属性设为必需并再次迁移。

【讨论】:

  • 这可能是正确的答案。您需要离开必需的属性,迁移,设置现有列的值,添加必需的属性,再次迁移
  • 删除所需属性后仍然没有更新
猜你喜欢
  • 2013-12-29
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多