Context
ASP.NET application with EF Core 6, connecting to a SQL Server database.
The current application I'm working on makes use of a database with about 200 tables.
Recently, the need has arisen for our application to store Unicode characters. This means all of those tables need to have several of their columns' types changed from VarChar
(some char) to NVarChar
.
Many of those tables use Char
columns as primary key. Since many tables are linked, this means there are many Char
FK columns as well.
I managed to change the EF code to generate the migration which would change these types.
Edit: For some added context: the database gets used in an existing (production) application, which means any alterations are limited to those things that would impact functionality (changing a column to a similar type is OK, changing a name is not).
Issue
Unfortunately, the generated migration fails because it would alter PK and FK columns
SqlException The object 'FK__FOREIGN_KEY_NAME' is dependent on column 'COLUMN_NAME'
The most common solution suggested online is to change the migration to drop keys before altering a column, and restore them again after. Because of the size and complexity of the database, this would take a lot of time and effort, so I'm trying to avoid this route.
Are there any other possibilities to change column types while ignoring any constraints?
I've tried
- removing keys in code, both in
OnModelCreate
and attributes in the entity classes. This was a tedious job, which only resulted in an error because of the lack of keys. - disabling constraints using
ALTER TABLE ? NOCHECK CONSTRAINT ALL
, as described in this question. This did not prevent the aforementioned errors.