0

I have a database column "WantsReply" that logically holds a boolean (bit) but is unfortunately implemented as an integer. Any nonzero value means "yes" and zero means "no".

If I write

class Entry {
   [Column] public int WantsReply {get; set;}
}

in my model, and map it to a checkbox in the view using

Html.CheckBox( "WantsReply", View.Model.WantsReply )

then I get a conversion error when I submit the view. On the other hand if I write

[Column] public bool WantsReply {get; set;}

then the view submit works OK but then I get a different conversion error when I run a query like

from entry in Entries select entry;

How can I resolve this impedance mismatch so that both queries and submits work?

1 Answer 1

3

Instead of changing the type of the column property, why not map it to a new property?

public partial class Entry {
    public bool WantsReplyAsBool
    {
        get
        {
            return WantsReply != 0;
        }
        set
        {
            if (value)
            {
                WantsReply = 1;
            }
            else
            { 
                WantsReply = 0;
            }
        }
    } 
}

The integer property can be private, if you like. Use the bool property in your view.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.