C
C#4d ago
AlisterKB

creating migrations on an existing DB to add a column(efcore)

so I have a webapi project, dotnet 8 efcore 8 and postgres project was DB first and hence scaffolded. now I wanna add a property to an entity of mine AND/OR add a column to a table I have always done db first and never done code first and have never touched migrations. tried adding migration but doing so it wants to create all tables with updated columns. tables already exist and have data I'd rather not lose (though it wouldn't be end of the world as it is a personal project for learning) so wondering how can I add a column without dropping my tables and not updating my table with sql command scaffolding it again. Most resources I've found involve somewhat hacky way of adding migration and deleting the content in up and down methods in migration though I don't understand how that might help ?
7 Replies
Optimum
Optimum4d ago
Have you got ur context setup properly? I presume u dont have the 'snapshot'
Optimum
Optimum4d ago
Code First Migrations with an existing database - EF6
Code First Migrations with an existing database in Entity Framework 6
Optimum
Optimum4d ago
ur gonna still have a nightmare doing this we currently use db first (unfortunately)
AlisterKB
AlisterKBOP4d ago
I do not, since the beginning of the project I just scaffolded and that's it so just for sake of testing I created an instance of my db locally. then I also went to to a new branch in my repo I careted a migration and anually change up and dodwn just to update the column protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn<decimal>( name: "AmountInOriginalUnit", table: "payment", type: "numeric", nullable: true, defaultValue: 0.00m); // Safe default for decimal type } /// <inheritdoc /> protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropColumn( name: "AmountInOriginalUnit", table: "payment"); } and it worked would this be a disaster of a workaround? also if I have scaffolded should I immediately add a migration to avoid the hell im ar right now or what is the way to prevent this if I start dbfirst ? reading it now
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
AlisterKB
AlisterKBOP3d ago
@Optimum firstly, thanks for entertaining my question and providing the link. @TeBeCo afer trying the workaround to no avail, I have come to the conclusion that -ignorechanges is not available for efcore and was a feature of ef6 so my question is what is the right way to deal with a code base you have started with an existing DB? I would assume one has no choice but to scaffold. but would it make sense to add an empty migration (manually removing everything ) so you would have a starting for your future migration without nuking the DB ? or should I just keep changing the db through queries and keep scaffolding. also I believe going code first with migrations add a migration table to the DB where it keeps track of implemented migrations ? (i might be wrong never done code first before as I mentioned earlier) would adding empty migrations in the middle of a scaffolded project also add migration history table to the DB (if it adds a migration history table to begin with)
Unknown User
Unknown User3d ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?