我一直在使用asp.net mvc实体框架,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,Origin: Configuration). No pending explicit migrations. Running Seed method. PM>
编辑2
最后解决了这个问题.我上面的所有步骤都是正确的.除了我正在编辑我的模型类,而不是实际绑定到ReminderDb(EF对象)的实际数据类.当我用属性更新正确的课程后,每件事情都成功地工作.感谢所有回复的人帮忙!
解决方法
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 Migrations with an existing database
原文地址:https://www.jb51.cc/csharp/96457.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。