【发布时间】:2015-03-19 04:54:49
【问题描述】:
这个问题已经被多次以不同的方式提出过。我已经浏览了所有答案并花费了大量时间。我似乎无法正常工作。
我一直在研究 asp.net mvc Entity Framework,SQL Server 应用程序。我已经有一个现有的数据库、表等,并且一切正常。我只需要在表中添加一个新列。
所以.. 我在模型类中添加了一个属性,以便可以将列添加到表中。但没有成功。
所以我按以下顺序执行步骤。
-
在我的模型类中添加该字段。
[Required] public string EmailSubject{ get; set; } - 然后我删除我的 asp.net mvc 项目中包含 Configuration.cs 类的文件夹 Migrations
-
然后在包管理器控制台中,我成功发出以下命令。
Enable-Migrations -ContextTypeName AppointmentReminder.Data.ReminderDb -Force -
然后我将属性设置为true如下
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 对象)的实际数据类。在我用属性更新了正确的类之后,每件事都成功了。感谢所有提供帮助的人!
【问题讨论】:
-
虽然我已经回答了我认为的答案,但您可能应该显示您遇到的错误。
标签: c# asp.net asp.net-mvc entity-framework entity-framework-migrations