efcore icon indicating copy to clipboard operation
efcore copied to clipboard

ArgumentException in runtime with nvarchar type

Open JuanIrigoyen opened this issue 9 years ago • 3 comments

I receive an argument exception in runtime 'Data type 'nvarchar' is not supported in this form.' when run a query in EF 2.0

using (MaldivasBaseContext mbc = new MaldivasBaseContext())
{
   var data = from c in mbc.UsuariosDepartamentos select c.Usuario;
   foreach (var item in data)
   {
        Console.Write(item);
   }
}

Note that in this case, the query don´t refer to nvarchar fields because in the entity 'UsuariosDepartamentos' don´t have fields nvarchar. I suppose that is for the relation with 'Usuarios' entity that have nvarchar fields.

public partial class UsuariosDepartamentos
{
    public string Usuario { get; set; }
    public string Departamento { get; set; }

    public Usuarios UsuarioNavigation { get; set; }
}
Exception message:
System.ArgumentException occurred
  HResult=0x80070057
  Message=Data type 'nvarchar' is not supported in this form. Either specify the length explicitly in the type name, for example as 'nvarchar(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type.
  Source=Microsoft.EntityFrameworkCore.SqlServer

Steps to reproduce

Write a query that use a entity with nvarchar fields

Further technical details

EF Core version: 2.0 Database Provider: Sql Server 2016 Operating system: Windows 10 Pro IDE: Visual Studio 2017 15.3.3

JuanIrigoyen avatar Aug 31 '17 11:08 JuanIrigoyen

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

Are you still getting this exception after following these instructions, or is there something not clear about what the message says?

ajcvickers avatar Aug 31 '17 16:08 ajcvickers

This error is about a sample of my model for test the new 2.0 version. I have generated a new model using Scaffold and test it with a console program. I the previous version this not had errors. I don't understand because a need to change my model before using Scaffold-DbContext. Why in this version nvarchar is not support?

JuanIrigoyen avatar Aug 31 '17 20:08 JuanIrigoyen

@JuanIrigoyen The scaffolding issue is tracked by #9188. As described in that issue, scaffolding is generating "nvarchar" column types when it shouldn't be. The workaround (until we ship a fix and assuming you don't want to use pre-release nightly builds) is to remove/replace these in the generated code--usually this can be done with a pretty simple find/replace. So for example, if you have "nvarchar" types something like this:

entity.Property(e => e.Notes)
    .HasColumnType("nvarchar");

because scaffolding has created this for "nvarchar(4000)" types, then change in the generated code either like this:

entity.Property(e => e.Notes)
    .HasColumnType("nvarchar(4000)");

or like this:

entity.Property(e => e.Notes)
    .HasMaxLength(4000);

Note that the runtime behavior here has not changed between 1.1 and 2.0. It was not valid to do .HasColumnType("nvarchar") in 1.1 either. The only thing that has changed is that scaffolding in 2.0 has a bug such that it sometimes generates this.

ajcvickers avatar Aug 31 '17 21:08 ajcvickers