Not considering new changes when Adding Migrations in ASP.NET core
Asked Answered
F

2

0

Me and a friend are working on an ASP.NET Core project as teammate and sharing data using github, every time who pull changes from github, should delete the Migration folder in order to add migrations without getting any errors, otherwise, we get the following error:

There is already an object named AspNetRoles in the database. (entity-framework-core)

So we should delete Migrations Folder and commenting the infos out inside the Up methode after every pulling to get rid of this error when doing migrations.

Now Assume that I alredy have a table named Product.cs that has two property as follows:

 public class Product
 {
      [Key]
      public int ProductId { get; set; }
      [Display(Name = " date ")]       
      public DateTime CreateDate { get; set; }

and then my friend decide to add new Prop into Product.cs(for example: public DateTime Count { get; set; }), Now when I pull the changes from github(after deleting Migrations Folder), I do new migrations again, but now the entity 'Product.cs' doesn't contain any column named Count in SSMS, So What should I do to have the new done changes in my tables in my data base?

Faucet answered 28/12, 2021 at 20:21 Comment(2)
Here is a link about how to solve There is already an object named AspNetRoles in the database. (entity-framework-core) without delete the Migration folder.You can have a try.Euchromatin
I've already seen it, but it is not so helpful!Faucet
J
3

Make sure you the delete the tables from database from SQL server.Since you are deleting the migrations folder and creating a new migration, the table will be created freshly. And so that the table will be created freshly as per your latest properties.

And use,

dotnet ef migrations add InitialMigration
dotnet ef database update
Jaquith answered 16/7, 2022 at 5:31 Comment(0)
V
0

Migrations are a critical part of source code so you aren't supposed to delete them after every pull from git. you should continue development and create a new migration if necessary without deleting them. every time you pull from git you must apply new migrations in your database (you can do this manually or automatically) and then continue.

But in your scenario, because you deleted the migration folder must also delete the database and recreate it with new migration. the reason is when you delete the migration folder and create a new migration EF Core thinks the database never exists before so try to create everything from scratch but you already have the database so creation failed.

Valley answered 31/12, 2021 at 7:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.