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

Nullable bool with default value [contrib?] #1168

Closed
MJohnsson opened this issue Dec 11, 2018 · 4 comments
Closed

Nullable bool with default value [contrib?] #1168

MJohnsson opened this issue Dec 11, 2018 · 4 comments

Comments

@MJohnsson
Copy link

MJohnsson commented Dec 11, 2018

I have an issue where the below property is always returned as "true" even if it's null in the db. I guess this is due to the default value, but default values shouldn't override what's in the db - or so I thought.

Part of model..
public bool? SolvedByCalldesk { get; set; } = true;

Part of Contrib-call..

...
var returnTask = connection.GetAsync<T>(id);
//caching here
result = await returnTask;
...

I've verified that the DB-record in question is null, but "result" returns it as true. Is this intended?

@EminemJK
Copy link

EminemJK commented Jan 3, 2019

No, you can try deleting
= true;
=>
public bool? SolvedByCalldesk { get; set; }
and see what happens

@NickCraver
Copy link
Member

It is indeed behaving as intended. You can use a constructor approach instead if you want to override the behavior (just have a constructor that matches the columns you're pulling back) if you want complete control here :)

@mcrepssdi
Copy link

This is not a correct answer when your object has over 200 parameters. Why would Dapper ignore nulls when reading from a DB. Was the intent for Dapper to only be used with a modern database and not a legacy database?

@mgravell
Copy link
Member

mgravell commented Nov 5, 2024

In many cases in Dapper "core", an important consideration is simply: changing this globally is basically impossible without breaking huge numbers of people, and offering per-location configuration is very complicated. If we want to do something here, I would suggest (as per #1909) that this is a feature that could be implemented much more sensibly in DapperAOT, possibly via a new property on [DbValue], or possibly as a new bespoke attribute that could be applied at multiple levels (field/property/type/module/etc). Your Dapper code doesn't change, other than to turn it on. If this is of interest, I would suggest logging it over there

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

5 participants