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.UnbufferedEntityShaper
1.Shape(QueryContext queryContext, ValueBuffer valueBuffer) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable
1.Enumerator.BufferlessMoveNext(Boolean buffer) at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func
3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__17
2.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Collections.Generic.List
1.AddEnumerable(IEnumerable1 enumerable) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 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 ?
User contributions licensed under CC BY-SA 3.0