16

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.

  1. Add the field in my model class.

    [Required]
    public string EmailSubject{ get; set; }
    
  2. Then I delete the folder Migrations in my asp.net mvc project containing Configuration.cs class
  3. Then in Package Manager Console, I issue following command successfully.

    Enable-Migrations -ContextTypeName AppointmentReminder.Data.ReminderDb -Force
    
  4. Then I set the property true as follows

    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }
    
  5. 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!

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
dotnet-practitioner
  • 13,968
  • 36
  • 127
  • 200

2 Answers2

16

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

Community
  • 1
  • 1
Will Eddins
  • 13,628
  • 5
  • 51
  • 85
7

You decorated the new column with required attribute. If that table has already some rows those rows can not have that column. Remove required. Than do migrations, populate all rows with data. Make property required and migrate again.

tmg
  • 19,895
  • 5
  • 72
  • 76