Map a property in the entity framework to a different type
I have a SQL Server 2008 database. I have a bunch o开发者_JAVA技巧f fields in TableA that are just strings that corresponds to booleans. So every value is either true or false. The edmx I generated using Entity Framework 4.0 has them as strings. This is technically correct but I would like to have them mapped as Booleans instead. Is this possible? If so how can I accomplish this?
Thanks much!
You could create a partial class alongside the generated one and add the bool property there with code to go back and forth from bool to string version. You could also mark the generated property as protected or internal to hide it from the rest of your code.
It's not ideal since the bool property cannot appear in query expressions unless you first force the query to happen using, for example, .ToList().
Your best bet would be to fix the database.
精彩评论