nvarchar(max) for identity columns creates issues with indexes
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?
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.
Exception thrown: 'System.AggregateException' in EntityFramework.Core.dll
@esargent there are two issues you are hitting here.
- Firstly, if you configure a foreign key it does not automatically pick up the length etc. of the key it is pointing too. We agree that it should, and https://github.com/aspnet/EntityFramework/issues/2455 is tracking that.
- In the meantime, configuring the length of the FK property is the correct thing to do, but you are hitting a bug (https://github.com/aspnet/EntityFramework/issues/2455) that prevents this from working since the property is on a derived type.
Until https://github.com/aspnet/EntityFramework/issues/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.