You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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);
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
The text was updated successfully, but these errors were encountered:
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.
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.
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.)
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);
The runtime error occurs in the following C# code:
namespace ConsoleApp_EFCore
{
class MainProgram
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
Further technical details
EF Core version: 2.0
Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Operating system: Windows 8
IDE: VS2017
The text was updated successfully, but these errors were encountered: