Entity Frame Core ExecuteUpdate Compiling error

Ning Zhu 131 Reputation points
2024-10-06T21:22:19.8766667+00:00

The following codes are all working great!

string? newval = null;
indexDB.TabeABC
    .Where(b => b.PropertyABC != null)
    .ExecuteUpdate(setters => setters.SetProperty(b => b.PropertyABC, newval));

However, this does not even pass compiler.

indexDB.TabeABC
    .Where(b => b.PropertyABC != null)
    .ExecuteUpdate(setters => setters.SetProperty(b => b.PropertyABC, null));

I do not understand why though, could someone give me a little explanation.

Thanks a million!

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
750 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 2,465 Reputation points Microsoft Vendor
    2024-10-07T06:06:31.1766667+00:00

    Hi,@Ning Zhu. Welcome to Microsoft Q&A. 

    As the compilation error message describes, your second way of writing will be ambiguous.

     

    SetProperty has overloaded methods.

    
    .SetProperty<TProperty>(Func<TSource, TProperty>, Func<TSource, TProperty>)
    
     
    
    .SetProperty<TProperty>(Func<TSource, TProperty>, TProperty)
    
    

    null could be assigned to both Func<TSource, TProperty> and TProperty. The compiler cannot identify which function to execute, so it reports an error of CS0121.

     

    As described in the CS0121 document, forced conversion could solve this problem.

    
    indexDB.TabeABC
    
        .Where(b => b.PropertyABC != null)
    
        .ExecuteUpdate(setters => setters.SetProperty(b => b.PropertyABC, (string)null));
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.