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

Dynamic type parameters: Cannot cast System.Int32 into any valid DbType after upgrading from 2.0.14.3 to 2.1.2.0 #201

Closed
gdau opened this issue Mar 28, 2014 · 9 comments
Labels
Milestone

Comments

@gdau
Copy link

gdau commented Mar 28, 2014

Hi,

After upgrading to 2.1.2.0 I am getting an error "Can't cast System.Int32 into any valid DbType." originating from the setter "public override Object Value" within NpgsqlParameter.cs.

I am using dapper.net and I have calls like this:

var template = builder.AddTemplate(@"
SELECT * FROM users
LIMIT :limit",
new
{
    limit = null as object
 });

This is stripped-down so doesn't necessarily make sense in this context, but I can fix it if I change

limit = null as object

to

limit = null as Int32?

Is the previous method supported? I just wanted to make sure this is by-design before I go about changing all the places where this is used. I have confirmed that in 2.0.14.3 this code works 100%.

Kind Regards,

Gav

@franciscojunior
Copy link
Member

Hmmmmm, thus isn't right. It should work. Do you think it would be possible to get how dapper is setting the parameter value? I never used dapper but I will try to set it up so I can try to reproduce this problem.
Thanks in advance.

@gdau
Copy link
Author

gdau commented Mar 29, 2014

Hi,

Thanks for getting back to me. I have attached a very simple console app that repro's this issue (you only need to put in your own connection string and any valid table name inside Program.cs).

If you change the reference to use the version of npgsql inside the included "original_working_lib" folder you will see the same code works fine (2.0.14.3).

You can download the sample app from here:

http://www.bitdiff.com/npgsqlbugdemo.zip

Thanks for taking a look.

Gav

@franciscojunior
Copy link
Member

Thanks for your test, @gdau !

But I got an even simpler test case which shows the problem:


NpgsqlParameter p = new NpgsqlParameter();
p.DbType = DbType.Object;

p.Value = 1;

I'm checking that.

It seems to have been caused by this commit: eeed751

@franciscojunior
Copy link
Member

Hi, @glenebob , I noticed that this problem is caused by the fact that when setting a parameter value, Npgsql tries to check the type of the parameter.

This code is having problem in the following line:

if (backendTypeInfo == null && !NpgsqlTypesHelper.TryGetBackendTypeInfo(type_info.Name, out backendTypeInfo))

It seems that the NpgsqlTypesHelper.TryGetBackendTypeInfo isn't finding the corresponding typeinfo. This name is text_nonbinary.

I notice that when you set the dbtype of the parameter to DbType.Object, Npgsql is setting the typeinfo to a type called text_nonbinary:

nativeTypeMapping.AddType("text_nonbinary", NpgsqlDbType.Text, DbType.Object, true);

which doesn't have a correspondence in the backend type info and thus it is throwing the exception.

Can you, please, talk more about this text_nonbinary mapping? Thanks in advance.

@franciscojunior
Copy link
Member

Hi, @glenebob !

I tested here and using one of your fixes from #102 ( using "unknown" instead of "text_nonbinary" ) fixes this issue (201).

Do you want to create a pull request to fix #201 or can I create one?

@glenebob
Copy link
Contributor

glenebob commented Apr 4, 2014

I can do that this weekend. I'll be working in Npgsql a little. I need
to back port those recent query rewrite fixes to 2.1.

-Glen

On Fri, Apr 4, 2014 at 12:57 PM, Francisco Figueiredo Jr. <
notifications@github.com> wrote:

Hi, @glenebob https://github.com/glenebob !

I tested here and using one of your fixes from #102#102 using "unknown" instead of "text_nonbinary" ) fixes this issue (201).

Do you want to create a pull request to fix #201https://github.com/npgsql/Npgsql/issues/201or can I create one?

Reply to this email directly or view it on GitHubhttps://github.com//issues/201#issuecomment-39604997
.

@franciscojunior
Copy link
Member

Great! I'm also investigating the #146
I'll let you know when I find anything. I already found somethings about it and added my comments there. If you can have a look at them, it would be awesome!

@danzel
Copy link
Contributor

danzel commented May 6, 2014

Here is a bunch of test cases:
danzel@91a74f3

danzel added a commit to danzel/Npgsql that referenced this issue May 6, 2014
@franciscojunior
Copy link
Member

Fixed by #230 .

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

No branches or pull requests

4 participants