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

Why isn't .HasMaxLength(X) being generated for nvarchar(X) in my model? #9521

Closed
averyoo7 opened this issue Aug 22, 2017 · 4 comments
Closed

Comments

@averyoo7
Copy link

Description

I'm getting a runtime error because of the model/scaffold/context (not sure which term is correct here) that's being generated. I haven't gotten out of the sandbox yet. I'm trying to build a DB-first model from SQL Server 2012 database.
If the length of a field is specified in the db table, and a max length is required to avoid runtime errors, then why doesn't EF Core include .HasMaxLength(X) in the generated model? Having to go back to the context--or anywhere--and manually add lines of code defeats the purpose of generated code. Is there a DataAnnotation, other switch, or a configuration I can use? Is "max" instead of "4000" getting in the way? (the table script below was generated by SQL Server.)

###Exception message:
System.ArgumentException: '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.'
Stack trace:
 	[External Code]		Annotated Frame
>	ConsoleApp_EFCore.dll!ConsoleApp_EFCore.MainProgram.Main(string[] args) Line 16	C#	Symbols loaded.

Steps to reproduce

I started with a database that includes this table:
CREATE TABLE [reference].[ActionTaken](
[unqID] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[txtCode] nvarchar NULL,
[txtName] nvarchar NULL,
[txtDescription] nvarchar NULL,
[intMask] [int] NULL,
[bolActive] [bit] NOT NULL,
[datCreated] [datetime] NULL,
[datModified] [datetime] NULL,
[txtSignatureModifiedBy] nvarchar NULL,
[txtSignatureCreatedBy] nvarchar NULL,
[rwvVersion] [timestamp] NOT NULL,
CONSTRAINT [PK_ActionTaken_unqID] PRIMARY KEY CLUSTERED ([unqID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [reference].[ActionTaken] ADD CONSTRAINT [DF_ActionTaken_unqID] DEFAULT (newsequentialid()) FOR [unqID]
GO
ALTER TABLE [reference].[ActionTaken] ADD CONSTRAINT [DF_ActionTaken_bolActive] DEFAULT ((0)) FOR [bolActive]
GO

In the Package Manager Console, I ran:
PM> Scaffold-DbContext "data source=[my server name]\SQLSERVER2012;initial catalog=[my database name];persist security info=True;user id=sa;password=[the password];MultipleActiveResultSets=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

...which created my class.cs and [db]Context.cs files in ConsoleApp_EFCore\Models folder.
In dbContext.OnModelCreating(), the following was created:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity(entity =>
{
entity.HasKey(e => e.UnqId);

            entity.ToTable("ActionTaken", "reference");

            entity.Property(e => e.UnqId)
                .HasColumnName("unqID")
                .HasDefaultValueSql("(newsequentialid())");

            entity.Property(e => e.BolActive)
                .HasColumnName("bolActive")
                .HasDefaultValueSql("((0))");

            entity.Property(e => e.DatCreated)
                .HasColumnName("datCreated")
                .HasColumnType("datetime");

            entity.Property(e => e.DatModified)
                .HasColumnName("datModified")
                .HasColumnType("datetime");

            entity.Property(e => e.IntMask).HasColumnName("intMask");

            entity.Property(e => e.RwvVersion)
                .IsRequired()
                .HasColumnName("rwvVersion")
                .IsRowVersion();

            entity.Property(e => e.TxtCode)
                .HasColumnName("txtCode")
                .HasMaxLength(50);

            entity.Property(e => e.TxtDescription).HasColumnName("txtDescription");

            entity.Property(e => e.TxtName)
                .HasColumnName("txtName")
                .HasMaxLength(100);

            entity.Property(e => e.TxtSignatureCreatedBy)
                .HasColumnName("txtSignatureCreatedBy")
                .HasMaxLength(256);

            entity.Property(e => e.TxtSignatureModifiedBy)
                .HasColumnName("txtSignatureModifiedBy")
                .HasMaxLength(256);
        });
    //...
    }

The runtime error occurs in the following C# code:
namespace ConsoleApp_EFCore
{
class MainProgram
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");

        using (dbContext mycontext = new dbContext())
        {
            var v = mycontext.ActionTaken.ToList(); // <--ERROR HERE
            foreach (ActionTaken a in v)
            { Console.WriteLine("name='" + a.TxtName + "'; desc = '" + a.TxtDescription + "'; code = '" + a.TxtCode + "';"); }
        }
    }
}

}

Further technical details

EF Core version: 2.0
Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Operating system: Windows 8
IDE: VS2017

@smitpatel
Copy link
Contributor

duplicate of #9188

@averyoo7
Copy link
Author

averyoo7 commented Aug 22, 2017

I see I'm wrong about the .HasMaxLength not being generated--it's there. I don't why I'm getting the error then, and I don't know the best place to ask about it.
Edit: I see. Thank you.

@smitpatel
Copy link
Contributor

@averyoo7 - Just change nvarchar to nvarchar(4000) and it will start working.

@ajcvickers
Copy link
Member

Hi @averyoo7. We are gathering information on the use of EF Core pre-release builds. You reported this issue shortly after the release of 2.0.0 RTM. It would be really helpful if you could let us know:

  • Did you consider testing your code against the pre-release builds?
  • Was there anything blocking you from using pre-release builds?
  • What do you think could make it easier for you to use pre-release builds in the future?

Thanks in advance for any feedback. Hopefully this will help us to increase the value of pre-release builds going forward.

@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
Projects
None yet
Development

No branches or pull requests

3 participants