efcore.pg: Migration error: type "nvarchar" does not exist
My class
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
}
Sql gerated by update-database
CREATE TABLE "Categories" (
"Id" int NOT NULL,
"Name" nvarchar(max) NULL,
CONSTRAINT "PK_Categories" PRIMARY KEY ("Id")
);
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (7 by maintainers)
dotnet ef migrations add InitialCreatedotnet ef database updateHere is my case where I had this issue, maybe it helps someone else.
The moral of the story: recreate your migrations when you change the setup before applying them!
This is all instances of SQL Server types being used in PostgreSQL, probably due to a misconfiguration. Another reason could be that you’re manually specifying column types in your model, specifying that your string property should be mapped to a column of type nvarchar. If that’s the case, you must add a condition in your model initialization code that SQL Server is being used, to avoid it from being applied to PostgreSQL.
In PostgreSQL strings should generally be mapped to columns of type
text. These columns are implicitly unlimited in length, there’s no need to add parentheses or anything.In my case I was trying to switch the Identity database to use Postgres. Having already generated the migrations for SQL Server by default it was failing with the above error. To fix it I did:
dotnet ef migrations remove- this removes the last migration, which in my case since there was only one, basically reset the migrationsdotnet ef migrations add [migration name]- I just copy-pasted the original migration name00000000000000_CreateIdentitySchemadotnet ef database updatewhich works pretty similar to Ben’s solution above 😃 just with the remove command instead of deleting the Migrations folder
nvarchar is an SQL Server type, not a PostgreSQL type. You mostly likely have a misconfiguration, follow the getting started instructions carefully. If you still can’t get it to work, post a code sample.