AlwaysEncrypted in ASP .Net Core 2.2 gets error Byte[]

1

I'm trying to use 'Always Encrypted' for ID client column in my table. Creating it in the SQL server was so easy. I tried to query in SSMS by activating in "Additional Connection Parameters" and type Column Encryption Setting=enabled. Run simple query and it works.

But I get errors in my running application. I also have added this 2 libraries to the project: Microsoft.Data.SqlClient & Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider

My code uses dependency injection.

These errors message:

System.InvalidOperationException HResult=0x80131509 Message=An exception occurred while reading a database value for property 'ID_Client'. The expected type was 'System.String' but the actual value was of type 'System.Byte[]'. Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueException[TValue](Exception exception, Object value, IPropertyBase property) at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.TryReadValue[TValue](ValueBuffer valueBuffer, Int32 index, IPropertyBase property) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.UnbufferedEntityShaper1.Shape(QueryContext queryContext, ValueBuffer valueBuffer) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.BufferlessMoveNext(Boolean buffer) at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__172.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext() Inner Exception 1: InvalidCastException: Unable to cast object of type 'System.Byte[]' to type 'System.String'.

From errors message above, it gives message that unsuitable casting between 'System.Byte[]' to 'System.String'. After I modified my model to Byte [], the error message becomes

Unable to cast object of type 'System.String' to type 'System.Byte[]'.

In ConnectionString, I've added "Encrypt=True; TrustServerCertificate=True"

This is my original model

public class Select_Clients {
[Key]
public Int32 ID { get; set; }
public string Client_Name { get; set; }
public string Client_ID{ get; set; }
}

how to overcome this error ?

I also get ref from this youtube and their code just works fine by replacing System.Data.SqlClient with Microsoft.Data.Client. Or, do I miss something ?

sql-server
asp.net-core
azure-keyvault
always-encrypted
asked on Stack Overflow Dec 9, 2019 by Daleman • edited Dec 9, 2019 by Daleman

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0