Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First Migration - Entity Framework - unable to add column to the table

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!

like image 922
dotnet-practitioner Avatar asked Jan 20 '15 20:01

dotnet-practitioner


People also ask

How do I add a column in code first approach in .NET core?

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.

Why add migration is not working?

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.


1 Answers

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

like image 146
Will Eddins Avatar answered Sep 19 '22 15:09

Will Eddins