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 ?

Daleman
  • 794
  • 8
  • 23
  • Have you referred to https://learn.microsoft.com/en-us/azure/sql-database/sql-database-always-encrypted-azure-key-vault?tabs=azure-powershell? – Jim Xu Dec 09 '19 at 06:37
  • @JimXu, that link is my ref as well. What the difference is I'm using Dependency Injection, meanwhile that ref using StringBuilder. I'm just trying to use StringBuilder for above issue. Let me come back later.... – Daleman Dec 09 '19 at 07:40
  • @jimXu, It seems I have to follow the guide line link but I just wonder there is no example for dependency injection. All examples still use old connection method (stringbuilder). – Daleman Dec 11 '19 at 22:22
  • @dalemen could you please provide your project? Let me test it. – Jim Xu Dec 12 '19 at 01:20

0 Answers0