Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter count mismatch error when running InsertGetIdAsync<int> #331

Closed
hussamelvani opened this issue Jan 6, 2020 · 9 comments
Closed

Comments

@hussamelvani
Copy link

I am using the following line of code to insert a record into DB, employeesInformation parameter is of type Dictionary<string, object> and has 33 parameters.

await sqlKataDB.Query("TBL_EMPLOYEES_INFORMATION").InsertGetIdAsync<int>(employeesInformation);

I am getting Parameter count mismatch error. As far as I have seen online, this error occurs when you Invoke a delegate function with wrong parameters (I am not sure here). I tried making the fuction sync instead of async but that didn't help.

I saw an open StackOverFlow ticket with the same error but with no answer, the OP was intructed to get RawSql and Bindigns, I can get you that if that helps (But I don't know how, instructions would be great!)

Any suggestions?

@ahmad-moussawi
Copy link
Contributor

could you please post a reproducible example?

@hussamelvani
Copy link
Author

ParameterCountMismatch.zip

I have attached the code that produces the error for me, I hope it is clear enough.

@ahmad-moussawi
Copy link
Contributor

@hussamelvani I still can't reproduce it, can't help without sharing a workable project so I can investigate it more,
mentioning the Database, Compiler and SqlKata version used.

@hussamelvani
Copy link
Author

@ahmad-moussawi I will try to reproduce it in a new project and get back to you because it is in a very big project that i unfortunately cannot share.
I will get back to you later with more information hopefully.
-Database: Microsoft SQL Server 2014.
-Compiler: SqlServerCompiler.
-SQLKata version: I tried 1.1.7 (latest stable at this time) through latest nightly 2.0.2 (latest at this time) with the same result.

@bmcmillan
Copy link

The issue is that the Dictionary is being cast down to an object going from the async extension execution class to the Query.Insert.AsInsert method and then the object is trying to be converted back into a dictionary, calling reflection methods on that dictionary thinking it's an object with properties that may have data annotations.

I think you just need to add an overload method explicitly for that dictionary type.

@hussamelvani
Copy link
Author

@bmcmillan I solved the issue by removing the Dictionary and typing the parameters one by one instead of passing them as a parameter, and that basically solved my problem.

@RichardAnderson
Copy link
Contributor

RichardAnderson commented Jan 26, 2020

Hi,

Getting the same issue.
This did not happen prevously, but when upgrading a project to .net core 3.0 and the latest stable release of SQLKata, I get the same issue.

Code as below:

       public async Task Create(EntityBase record)
        {
            // Create a new Dictionary
            // with all the metadata
            var data = new Dictionary<string, object>(record.Values);

            // Remove fields we do NOT want the user to set:
            data.Remove(EntityBase.CoreAttributes.Id);

            // Run the query... 
            var response = await Query()
                .From(record.TableName)
                .InsertGetIdAsync<int>(
                    data
                );

            // Return the response...
            record.Id = response;

            // Finally.. Clear out the changes!
            record.Changes = null;
        }

As others have said above - this is due to a dictionary being passed in (cannot replicate if I pass in a dynamic object).

Any suggestions on how to overcome this issue? Changing from a Dictionary is not an option for me in this project.

RichardAnderson added a commit to RichardAnderson/querybuilder that referenced this issue Jan 26, 2020
@RichardAnderson
Copy link
Contributor

Sumitted a Pull request - I have confirmed the changes made resolve the issue.

@ahmad-moussawi
Copy link
Contributor

Thanks guys, I merged @RichardAnderson's PR now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants