11

这个问题已被多次提出在SO上的不同口味。我经历了所有的答案,并且耗费了大量的时间。我似乎无法得到这个工作。代码优先迁移 - 实体框架 - 无法将列添加到表

我一直在研究asp.net mvc实体框架,SQL服务器应用程序。我已经有了一个现有的数据库,表格等等,而且每件事情都在起作用。我只需要在表格中添加一个新列。

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

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

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

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

    Enable-Migrations -ContextTypeName AppointmentReminder.Data.ReminderDb -Force 
    
  4. 然后,我设置属性真如下

    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对象)的实际数据类。在我用属性更新了正确的类后,每件事情都成功了。感谢所有那些回应帮助的人!

+0

虽然我已经回答了,我认为答案是什么,你应该表现出什么样的错误你得到。 – 2015-01-20 20:28:35

回答

11

因为该字段是必需的,所以您需要指定一个默认值。编辑您的迁移文件,找到这一行加入你的专栏,并指定默认值应该是什么:

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

+0

删除所需的属性后仍然没有更新 – 2015-01-20 21:30:42

+0

@ dotnet-practitioner您'不要在任何地方进行“添加迁移”,所以它没有任何东西可以应用。看到我上面的更新。 – 2015-01-20 21:34:59

5

您用新的属性装饰了新列。如果该表已经有一些行,则这些行不能包含该列。 需要移除。与迁移相比,使用数据填充所有行。 设置属性并重新迁移。

+2

这可能是正确的答案。您需要删除所需的属性,迁移,设置现有列的值,添加必需的属性,再次迁移 – pquest 2015-01-20 20:20:46

+0

删除所需的属性后仍然没有更新 – 2015-01-20 21:29:13