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)

Most upvoted comments

  1. Delete your “Migrations” folder.
  2. Run dotnet ef migrations add InitialCreate
  3. Then dotnet ef database update

Here is my case where I had this issue, maybe it helps someone else.

  1. I first faultily configured my EF to work with MSSQL
  2. I created the migrations
  3. I tried fixing everything to work with Postgres and succeeded
  4. I tried applying migrations without recreating them This caused the issue as the migrations contain database-specific information and were applying MSSQL constructs to Postgres.

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 migrations
  • dotnet ef migrations add [migration name] - I just copy-pasted the original migration name 00000000000000_CreateIdentitySchema
  • dotnet ef database update

which 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.