0

Is there anyway to do this :

I have a table called FOO and I want to auto cast a char(10) field in SQL to a bool in c# without creating an additional override fake GET property to get the bool CAST of the SQL field. Is there a way using like a custom attribute over the IsPremium c# field or a way using FluentAPI?

TABLE FOO
{
   INT Id;
   CHAR(10) IsPremium;
}

and I want my c# entity model to be like this

class FOO
{
   int Id { get;set;}
   bool IsPremium { get;set;}
}
kyleb
  • 1,968
  • 7
  • 31
  • 53
  • I have to say it seems like you're solving the wrong problem in this case. – Casey Oct 21 '14 at 16:23
  • why in the table you don't want to have a bit instead of char(10)? – Eru Oct 21 '14 at 16:24
  • I know its ugly. But it is what I need to do for now, we plan on refactoring at a later time. Legacy business and time schedule reasons. – kyleb Oct 21 '14 at 16:24
  • This will probably do what you want, don't think there's another way [Convert value when mapping](http://stackoverflow.com/questions/19370104/convert-value-when-mapping) – DavidG Oct 21 '14 at 16:30
  • Yes I thought of this solution already. I was looking for something a little more elegant using an auto cast attribute. I am a little confused on the EF6 solution in the link you sent me though. What is it trying to accomplish? Does it mean to create a bool IsActive property and that FluentAPI code fills it in via the SET in the Property? – kyleb Oct 21 '14 at 16:34
  • 1. Add to database field (IsPremiumB bit), with update trigger from IsPremium to IsPremiumB. 2. Create and map to view [link](http://stackoverflow.com/questions/16890505/mapping-sql-view-in-code-first-approach) 3. Use Raw SQL [link] (http://msdn.microsoft.com/en-us/data/jj592907.aspx) – Alexandr Sulimov Oct 25 '14 at 20:54
  • Nice idea but not exactly what I am looking for. Trying to accomplish without SQL changes. But I fear there is not a way really or MS wants us to do it via db schema change. Sometimes we can't always change a schema for one reason or another. – kyleb Oct 26 '14 at 01:45

0 Answers0