Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentException in runtime with nvarchar type #9656

Closed
JuanIrigoyen opened this issue Aug 31, 2017 · 3 comments
Closed

ArgumentException in runtime with nvarchar type #9656

JuanIrigoyen opened this issue Aug 31, 2017 · 3 comments
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@JuanIrigoyen
Copy link

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

@ajcvickers
Copy link
Member

@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?

@JuanIrigoyen
Copy link
Author

JuanIrigoyen commented Aug 31, 2017

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?

@ajcvickers
Copy link
Member

@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 ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Aug 31, 2017
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

2 participants