Convert from to string in database to boolean property Entity Framework 4.1

10,962

Solution 1

I would suggest wrapping up or extending the generating type to add this sort of functionality ...

entity framework will generate objects that basically match what the data in the database tables looks like so if you have a table called "Contacts" you'll get an object called "Contacts", I think (although i could be wrong) the classes are defined as partial so it generates something like ...

public partial class Contact { 
  string BoolReally { 
    get; set;
  };
}

you then add a new property like this ...

public partial class Contact { 
  bool MyBool get { 
    return (legacyValue == "T") ? true : false;
  }
}

Now when you declare a Contact instance just fish the value from "MyBool" instead.

...

That's extending, wrapping up would be something like this ...

public class MyContact {
     public Contact Contact;
     public bool MyBool { 
         get { 
             return Contact.BoolAsString; 
         }
     }
}

similar thing ... just need to consume the object slightly differently :)

Solution 2

It's not possible. You will have to map a string property to the database table column and then use a not mapped boolean property:

public string MyStringProperty { get; set; }

[NotMapped]
public bool MyBoolProperty
{ 
    get { return MyStringProperty == "T"; }
    set { MyStringProperty = value ? "T" : "F"; }
}
Share:
10,962
Chris McGrath
Author by

Chris McGrath

I live and work in Toronto I am currently an intermediate software developer specializing in C# Web Development and have been working with Climax Media since October 2014

Updated on June 04, 2022

Comments

  • Chris McGrath
    Chris McGrath almost 2 years

    i am working on a legacy oracle database that uses character literals T and F in the database for its boolean values however i would like the Entity property to reflect a proper boolen value is there a wy to convert this value when the model is binding it is a read only database so inserts are not important

  • Chris McGrath
    Chris McGrath almost 13 years
    damn i was really hoping to keep the model POCO ignorant to the mapping to the database would be really nice if there was a way to do this in the property map with the fluent api
  • Ladislav Mrnka
    Ladislav Mrnka almost 13 years
    @Chris: Tell it to ADO.NET team. They still ignore needs for simple type mapping.
  • Steven
    Steven almost 11 years
    Downside of this is that the MyBool property can't be used in a LINQ query, since EF can't translate such query to SQL.
  • War
    War almost 11 years
    there's nothing stopping you using the same logic in a linq query on the older string property though. Bit of a nasty workaround though. My ideal would be to create a calculated field in the DB so you get a bit column translated in to your POCO / entity which can be used in linq.
  • War
    War almost 11 years
    or just convert the column with a few SQL updates (1 to the table, 1 to copy the data in to a bit col)
  • Steven
    Steven almost 11 years
    Instead of using properties, I only define methods (even for property like behavior). This makes it more obvious that that thing can't be used in a LINQ query.
  • War
    War almost 11 years
    Good call. I guess this is another thing that comes down to coding standards, for you or your company each may have variation in areas like this.