Home > database >  C# .NET Cannot create table from migration-there is a synonym with the same name in the database
C# .NET Cannot create table from migration-there is a synonym with the same name in the database

Time:01-27

I want to create a .NET Service which receives information from a questionnaire and saves the information in a database. I created my model, repository, controller and service and when tested it for the first time it worked perfectly. Unfortunately the information has to be in another schema too. So my colleagues created a synonym in my schema which copies the table from my schema and pastes it in the other.

So they dropped my table, I deleted all migrations with the migration table in the DB and when tried to create the initial migraiton again and update the data-base, an error occurs that an object with the same name already exists.

Thank you in advance!

CodePudding user response:

You need to be somewhat careful when dealing with migrations. Migrations should be considered immutable, i.e. you can add new migrations but not change existing ones. This is not an absolute rule, but all existing databases that have applied the migration probably need to be thrown away if a migration is changed. So changing a migration before committing it is probably fine, changing it after committing it is probably a fair bit of work, and changing it after release is a big no-no.

So if your college added a migration correctly you should just need to run Update-Database to have everything work.

If you have messed with the migration table you should probably just delete the entire database and recreate it. When developing it is often useful to view your local database as disposable, and invest in tooling that makes recreating the database and adding default values easy.

  • Related