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

nvarchar(max) for identity columns creates issues with indexes #4226

Closed
esargent opened this issue Jan 5, 2016 · 2 comments
Closed

nvarchar(max) for identity columns creates issues with indexes #4226

esargent opened this issue Jan 5, 2016 · 2 comments

Comments

@esargent
Copy link

esargent commented Jan 5, 2016

I thought this might be fixed in rc2 when Issue 1071 was closed but it seems to still be an issue. Could also be related to Issue 4134. I experienced this in rc1-final and now in 7.0.0-rc2-16649

Use Case:
I've inherited several classes from ApplicationUser - such as Distributor and Customer. I want my Customer to have a primary distributor

   Distributor PrimaryDistributor {get;set;}
   string PrimaryDistributorId {get;set;}

What happens
The code compiles and I can generate a migration, but the migration script, which I can capture with the -o option, shows that Customer.PrimaryDistributorId is being created as nvarchar(max) which doesn't match ApplicationUser.Id which is nvarchar(450) so create index fails. (They are all being created in the AspNetUsers table, which is what I expected.)

I think I can fix that so add to my OnModelCreating()

        builder.Entity<ApplicationUser>()
            .Property(e => e.Id).HasMaxLength(450);

        builder.Entity<Customer>()
            .Property(x => x.PrimaryDistributorId).HasMaxLength(450);

which is what was suggested as a workaround in the discussion for 1071, but it still generates nvarchar(max) as the type, which causes an error when the script tries to create the index.

Not really clear if I am stuck here or if there is another workaround? Or am I doing something wrong?

@esargent
Copy link
Author

esargent commented Jan 6, 2016

update:

I hacked the column directly following instructions from (http://stackoverflow.com/questions/26950625/how-to-create-an-index-for-a-string-property-in-entity-framework-7). But that led to an issue where EF can't load a user - even a base applicationuser - out of the database when they are logged in or I try to enumerate them. It was able to create the user, but just can't get it back out. The application is working fine for access to all other objects.

Is it possible to tell from the stack trace which property it is hacking on?

Microsoft.Data.Entity.Query.Internal.SqlServerQueryCompilationContextFactory: Error: An exception occurred in the database while iterating the results of a query.
System.AggregateException: One or more errors occurred. ---> System.InvalidCastException: Specified cast is not valid.
at lambda_method(Closure , ValueBuffer )
at Microsoft.Data.Entity.Query.EntityLoadInfo.Materialize()
at Microsoft.Data.Entity.Query.Internal.QueryBuffer.GetEntity(IKey key, EntityLoadInfo entityLoadInfo, Boolean queryStateManager, Boolean throwOnNullKey)
at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.BufferedEntityShaper1.Shape(QueryContext queryContext, ValueBuffer valueBuffer) at Microsoft.Data.Entity.Query.AsyncQueryMethodProvider.<>c__DisplayClass3_01.<_ShapedQuery>b__0(ValueBuffer vb)
at System.Linq.AsyncEnumerable.<>c__DisplayClassab2.<>c__DisplayClassad.<>c__DisplayClassaf.<Select>b__a9(Boolean res) --- End of inner exception stack trace --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at Microsoft.Data.Entity.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext() ---> (Inner Exception #0) System.InvalidCastException: Specified cast is not valid. at lambda_method(Closure , ValueBuffer ) at Microsoft.Data.Entity.Query.EntityLoadInfo.Materialize() at Microsoft.Data.Entity.Query.Internal.QueryBuffer.GetEntity(IKey key, EntityLoadInfo entityLoadInfo, Boolean queryStateManager, Boolean throwOnNullKey) at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.BufferedEntityShaper1.Shape(QueryContext queryContext, ValueBuffer valueBuffer)
at Microsoft.Data.Entity.Query.AsyncQueryMethodProvider.<>c__DisplayClass3_01.<_ShapedQuery>b__0(ValueBuffer vb) at System.Linq.AsyncEnumerable.<>c__DisplayClassab2.<>c__DisplayClassad.<>c__DisplayClassaf.b__a9(Boolean res)<--- Exception thrown: 'System.AggregateException' in EntityFramework.Core.dll

@rowanmiller
Copy link
Contributor

@esargent there are two issues you are hitting here.

Until #2455 is fixed, you will need to manually edit the scaffolded migration to specify an explicit type to use.

            migrationBuilder.AddColumn<string>(
                name: "PrimaryDistributorId",
                table: "AspNetUsers",
                nullable: true,
                type: "nvarchar(450)");

I'm closing this out, since the fundamental issues are already tracked by other issues.

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

No branches or pull requests

3 participants