这个问题已被多次提出在SO上的不同口味。我经历了所有的答案,并且耗费了大量的时间。我似乎无法得到这个工作。代码优先迁移 - 实体框架 - 无法将列添加到表
我一直在研究asp.net mvc实体框架,SQL服务器应用程序。我已经有了一个现有的数据库,表格等等,而且每件事情都在起作用。我只需要在表格中添加一个新列。
所以..我在模型类中添加了一个属性,以便我可以将该列添加到表中。但没有成功。
所以我按照以下顺序执行这些步骤。
添加我的模型类中的字段。
[Required] public string EmailSubject{ get; set; }
- 然后我删除我的asp.net MVC项目包含Configuration.cs类
然后在包管理器控制台的文件夹迁移,我发出成功以下命令。
Enable-Migrations -ContextTypeName AppointmentReminder.Data.ReminderDb -Force
然后,我设置属性真如下
public Configuration() { AutomaticMigrationsEnabled = true; }
然后我发出的包管理器控制台执行以下命令。
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对象)的实际数据类。在我用属性更新了正确的类后,每件事情都成功了。感谢所有那些回应帮助的人!
虽然我已经回答了,我认为答案是什么,你应该表现出什么样的错误你得到。 – 2015-01-20 20:28:35