Search code examples
entity-frameworknvarchar

EF core to support nvarchar


I have an existing EF code first project which has created the database with all nvarchar columns in the database. Now I am starting another project on the same database for statistical purpose. This new project however is using EF core as pointing to same database. When I tried to run the new project, it gives me following error.

"Data type 'VARCHAR' is not supported in this form. Either specify the length explicitly in the type name, for example as 'VARCHAR(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type."

Now, as I already have production data in the database so I want to make minimum impact to the column types but still wan to use EC core in my new project. I have so many nvarchar columns so setting configuration on individual table is a hard job. Anyone can point me in the right direction?


Solution

  • This seems to be an issue with the 2.0 release of EntityFrameworkCore: https://github.com/aspnet/EntityFrameworkCore/issues/9188

    Supposed to get fixed in 2.1 so you may want to wait until then.

    Otherwise they suggest manually fixing it like this:

                entity.Property(e => e.Comments)
                    .HasColumnName("Comments")
                    .HasColumnType("nvarchar(4000)"); // <- Add this