views:

243

answers:

1

So I have a small issue with converting a string to a boolean when EF maps to my POCO. I created custom POCOs and I have one that has a boolean property called "IsActive". But, in the database the tables column "IsActive", that maps to the POCOs property, is a string. It's either 'Y' or 'N'.

EF doesn't like this, so I'm wondering if there's a way to tell it to convert the string to a boolean through a custom method?? Thanks!

+1  A: 

Have not tested it by myself. http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx

Create complex type definition in your edmx.

<ComplexType Name="IsActiveWrapper" > 
          <Property Type="string" Name="Value" Nullable="false" /> 
</ComplexType>

Create complex type

public class IsActiveWrapper
{
    private bool isActive;

    public string Value
    {
        get
        {
            return isActive ? "Y" : "N";
        }

        set
        {
            isActive = "Y".Equals(value);
        }
    }

    public bool IsActive
    {
        get { return isActive; }
        set { isActive = value; }
    }

    public static implicit operator IsActiveWrapper(bool isActive)
    {
        return new IsActiveWrapper { IsActive = isActive };
    }

    public static implicit operator bool(IsActiveWrapper wrap)
    {
        if (wrap == null) return false;
        return wrap.IsActive;
    }
}

Now you can do something like this

public class TestIsActive
{
    public virtual IsActiveWrapper IsActive { get; set; }
}
var test = new TestIsActive { IsActive = true };
Yury Tarabanko
The problem with such solution is that it doesn't play well with Linq queries..
Roger Alsing
Forget to point it... Thanks to @Roger AlsingIn queries you have to write something like thisvar query = from test in ctx.TestIsActive where test.IsActive.Value == "Y" select test;
Yury Tarabanko
So I wouldn't be able to set my Entity property to Boolean? That may be an issue....I thought about creating a tempIsActive property in my Entity that maps to the db string and then have my IsActive Entity property get the tempIsActive property value and convert it to a Boolean. But, I have just tied my Entity to my database...
Dan H
You would be able to set your Entity property to Boolean due to implicit operators, see the last line. As far as I know the only incovenience with this solution is with Linq queries.
Yury Tarabanko
Okay, I didn't notice that. I'll give it a try and see if it works for me. Thanks!
Dan H
I can't seem to get this to work. I have never worked with Complex Type so I'm sure I'm missing something. How do I link my IsActiveWrapper to my IsActive property on my Entity?
Dan H
I'm getting this error - "Schema specified is not valid. Errors: No corresponding object layer type could be found for the conceptual type 'TestProj.IsActiveWrapper'."
Dan H
I got it to work! I generalized IsActiveWrapper to BooleanWrapper and now I can use it on any of my Entities that have booleans. Thank you for your help!
Dan H