Error of the first code migration "Incorrect conversion from nvarchar (max) to varbinary (max) data type is not allowed"

I changed the data type of the field in my model from array of strings to bytes, and I got this error when I run the first migration method of the Update-Database code.

Implicit conversion from nvarchar (max) to varbinary (max) data type is not allowed. Use the CONVERT function to run this query

What's the solution?

thanks

+6
source share
1 answer

SQL Server cannot directly change a string to a binary, so it asks for data conversion. Regarding the First Code Migrations, I would drop the AlterColumn instructions from DbMigration, and instead manually write:

 AddColumn("dbo.TableName", "ColumnNameTmp", c => c.Binary()); Sql("Update dbo.TableName SET ColumnNameTmp = Convert(varbinary, ColumnName)"); DropColumn("dbo.TableName", "ColumnName"); RenameColumn("dbo.TableName", "ColumnNameTmp", "ColumnName") 

And the opposite is in the Down method, if necessary. (above is pseudo code, forgive any syntax errors)

+13
source

All Articles