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

System.InvalidCastException Object cannot be stored in an array of this type. #243

Open
azan-n opened this issue Aug 12, 2024 · 0 comments

Comments

@azan-n
Copy link

azan-n commented Aug 12, 2024

Summary of the issue

I recently added an enum model to a Postgres database as follows:

public enum Level {
  High,
  Medium,
  Low
}

public class Model {
  public Level[] AllowedModel {get; set;}
}

Environment

.NET (Core) SDK version: 8.0
Microsoft.EntityFrameworkCore version: 
EFCoreSecondLevelCacheInterceptor version: 4.5.0
Database: Postgres
Cache provider: Redis

Example code/Steps to reproduce:

public enum Level {
  High,
  Medium,
  Low
}

public class Model {
  public Level[] AllowedModel {get; set;}
}


// in DB Context OnModelCreating

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.HasPostgresEnum<Level>();
  ...
}

Output:

Exception message: System.InvalidCastException Object cannot be stored in an array of this type.
Full Stack trace: 

System.InvalidCastException: Object cannot be stored in an array of this type.
   at System.Array.InternalSetValue(object value, IntPtr flattenedIndex)
   at EFCoreSecondLevelCacheInterceptor.EFTableRowsDataReader.ProcessPostgresArrayOrList<T>(Type expectedValueType, IEnumerable enumerable) in D:\a\EFCoreSecondLevelCacheInterceptor\EFCoreSecondLevelCacheInterceptor\src\EFCoreSecondLevelCacheInterceptor\EFTableRowsDataReader.cs:line 613
   at EFCoreSecondLevelCacheInterceptor.EFTableRowsDataReader.GetFieldValue<T>(int ordinal) in D:\a\EFCoreSecondLevelCacheInterceptor\EFCoreSecondLevelCacheInterceptor\src\EFCoreSecondLevelCacheInterceptor\EFTableRowsDataReader.cs:line 512
   at lambda_method1676(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable<T>+AsyncEnumerator+<MoveNextAsync>d__20.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task, ConfigureAwaitOptions options)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions+<ToListAsync>d__67<TSource>.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions+<ToListAsync>d__67<TSource>.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task, ConfigureAwaitOptions options)
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

1 participant