Issue while setting varbinary(max) to null
Description
Not able to set varbinary to null using below code line: await db.DbTableEntity.Where(c => c.Id == Id).UpdateFromQueryAsync(x => new DbTableEntity { CompanyLogo = null });
Entity property: public byte[] CompanyLogo { get; set; }
Exception
Exception message: {"Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query."}
Stack trace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at .(DbCommand , BulkOperation , Int32 )
at .(DbCommand , BulkOperation , Int32 )
at .( )
at .Execute(List`1 actions)
at .(List`1 )
at Z.BulkOperations.BulkOperation.()
at Z.BulkOperations.BulkOperation`1.UpdateFromQuery(IQueryable originalQuery, IQueryable query, Expression`1 updateExpression)
at DbContextExtensions.[](IQueryable`1 , Expression`1 , Action`1 , Boolean )
at DbContextExtensions.UpdateFromQuery[TEntity](IQueryable`1 query, Expression`1 updateExpression, Action`1 bulkOperationFactory)
at DbContextExtensions.UpdateFromQuery[TEntity](IQueryable`1 query, Expression`1 updateExpression)
at DbContextExtensions.`1.()
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at DbContextExtensions.`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at xxxController.<RemoveLogo>d__21.MoveNext() in C:\Users\xxx\source\repos\xxxx\Controllers\XxxxController.cs:line 114
Further technical details
- EF version: EF 6
- EF Extensions version: <package id="Z.EntityFramework.Extensions" version="5.2.8" targetFramework="net48" />
- Database Provider: MS SQL Server
Hello @kpp-mantleanalytics ,
Thank you for reporting, we easily reproduced the issue here: https://dotnetfiddle.net/Bi0t5X
We will look at it if that's possible to fix it.
Best Regards,
Jon
Hello @kpp-mantleanalytics ,
At this moment, the default logic in EF6 (we need to keep it for backward compatibility) doesn't support it. However, the new logic (the one used in EF Plus and some complex scenario that we can discover support it with the option: option.InternalIsEntityFrameworkPlus = true or directly using the Update method in EF Plus.
context.Customers.Where(x => x.Counter >= 1).UpdateFromQuery(x => new Customer { CompanyLogo = null }, option => option.InternalIsEntityFrameworkPlus = true);
Fiddle: https://dotnetfiddle.net/i73OYc
Let me know if that worked with this options
Best Regards,
Jon
Thanks Jon, we will follow given instructions.
Hello @kpp-mantleanalytics ,
Since our last conversation, we haven't heard from you!
Did you get the chance to try the option?
Let us know if it fixed the issue.
Looking forward to hearing from you,
Jon
Hi Jon,
Thank you for aksing. Yes we have used options => options.InternalIsEntityFrameworkPlus = true. And its working fine.
Regards, Kiran
Awesome @kpp-mantleanalytics !
we are glad to hear that the options work!
Don't hesitate to contact us with any questions or further assistance :)
Best Regards,
Jon