This question has been asked many times in different flavors on SO. I have gone through all the answers and burn lot of time. I just can not seem to get this working.
I have been working on asp.net mvc Entity Framework, SQL server app. I already have an existing database, tables, etc. and every thing is working. I just need to add a new column to the table.
So.. I added a property in the model class, so that I could add the column to the table. But with no success.
So I do the steps in the following order.
Add the field in my model class.
[Required]
public string EmailSubject{ get; set; }
Then in Package Manager Console, I issue following command successfully.
Enable-Migrations -ContextTypeName AppointmentReminder.Data.ReminderDb -Force
Then I set the property true as follows
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
Then I issue the following command in package manager console.
Update-Database -Verbose -Force
Here is what my connection string to default connection to the database looks like
Data Source=(LocalDb)\v11.0;AttachDbFilename=C:\practice\AppointmentReminder4\AppointmentReminder4\App_Data\aspnet-AppointmentReminder4-20141202060615.mdf;Initial Catalog=aspnet-AppointmentReminder4-20141202060615;Integrated Security=True
But I am UNABLE to add the new column in my desired table.
Edit 1
I updated the model as follows without the required attribute and executed all of the above steps but I was still NOT able to add the column to the table.
public string EmailBody { get; set; }
Here are all the commands and their output.
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>
Edit 2 Finally resolved this issue. All my steps above were correct. Except I was editing my model class as opposed to actual data class that is actually tied to the ReminderDb (EF object). After I updated the correct class with the property, every thing worked successfully. Thanks to all those who responded with help!
Once you run the add-migration command, a new migration class will be created and opened, in that, you can see a new column has been added to the Employee table. This class contains the details of the newly added column.
Add-Migration - The Term 'Add-Migration' Is Not Recognized After creating models and context class, we nomally add migration to initialize the database. The error occurs sometimes while adding migration in asp.net core, entity framework with code first approach because of some missing library.
Because the field is required, you need to specify a default value. Edit your migration file, find the line adding your column, and specify what the default value should be:
public override void Up()
{
AddColumn("dbo.MyTable", "EmailSubject", c => c.String(nullable: false, defaultValue: ""));
}
For reference: Default value for Required fields in Entity Framework migrations?
Edit: Based on your edit, you need to create a migration before you try to update. See this example for how you typically use it.
However, if you're trying to apply Code First Migrations to an existing database, this article may help: Code First Migrations with an existing database
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With