0

I am trying to do a migration for my database to add a column. I set up the migration and everything looks good. I ran the command and it grabbed the right migration and ran it into the database, yet it is not showing up in the database.

My Migration:

 using System; using Microsoft.EntityFrameworkCore.Migrations; namespace Mudman.Data.Migrations { public partial class AttachedFileFileCategory : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn<string>( name: "FileCategory", table: "AttachedFile", type: "nvarchar(max)", nullable: true); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropColumn( name: "FileCategory", table: "AttachedFile"); } } 

}

my terminal of the commands:

Terminal

The Database:

Database

3
  • 1
    Are you sure it's the correct database? Also, did you refresh the app you are using to connect to the database? Try closing and reopening it. Commented Nov 12, 2021 at 19:15
  • @DavidG you're right, it actually went to our stage db and not our development.... is there a way in the future to specify which database you want when you're adding a column? Commented Nov 12, 2021 at 19:49
  • It uses the same db the context uses. If it went in your staging db it's because that was the db that would have been connected to by running the app with whatever config it had for the environment - see eg learn.microsoft.com/en-us/aspnet/core/fundamentals/… Commented Nov 12, 2021 at 19:55

2 Answers 2

1

I read in the comments that your 'problem' was the environment.
There are many ways to get around with what you want:

  1. The command you are using for migrations is part of Entity Framework Core tools. The docs, among other things, mention that you can specify the following option:

--connection <CONNECTION>
connection string to the database. Defaults to the one specified in AddDbContext or OnConfiguring. Added in EF Core 5.0.

Example:
dotnet ef database update your_migration --connection your_connection_string

OR

  1. In ConfigureServices, you can get the environment and conditionally register the dbContext with whatever connection string you want. How to get the Development/Staging/production Hosting Environment in ConfigureServices
Sign up to request clarification or add additional context in comments.

Comments

0

I know this question was posted forever ago. But I ran into this issue today. The solution was so simple.... naturally. I had to run the command update-database in the console. Worked perfectly after that.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.