Make VarChar(5) map to boolean?

6 posts, 0 answers
  1. Sean
    Sean avatar
    3 posts
    Member since:
    May 2014

    Posted 15 May 2014 Link to this post

    Ok so my company has a database (Microsoft SQL) That stores VarChar(5) with either True or False for boolean values.

    I am trying to implement an ORM (Preferably Telerik Data Access) and I need the code generator to produce boolean properties for Varchar(5) columns. 

    I have made a Telerik Data Access Class Library and Added a couple of our tables via the reverse mapping method and I have been trying to figure out how to accomplish this for a while now with no luck.

    Any help is appreciated.
  2. Ady
    Admin
    Ady avatar
    589 posts

    Posted 19 May 2014 Link to this post

    Hello Sean,

     You could manually change the CLR type of the property from 'string' to 'bool' in the designer and then write a custom converter that generates a true/false value based on the column value obtained from the database. This would be more complicated compared to adding a boolean property in the class and settings its value from the mapped string property.
    For example , if the designer generates a string property - 'StringProp' then you can add another prop - 'BoolProp' in a partial class and get/set the value based on the value of the 'StringProp'

    public bool BoolProp
          {
              get
              {
                  bool retValue = false;//default value
                  bool.TryParse(this.StringProp, out retValue);
     
                  return retValue;
              }
     
              set
              {
                  this.StringProp = value.ToString();
              }
          }

    Do get back in case you need further assistance.

    Regards,
    Ady
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Sean
    Sean avatar
    3 posts
    Member since:
    May 2014

    Posted 19 May 2014 in reply to Ady Link to this post

    Hi Ady,

    Thank you for getting back to me, I appreciate it. Unfortunately this solution does not exactly solve my problem. I am trying to avoid having to manually set properties after generation.

    Our database has about 250 tables and who knows how many properties there are in total. Manually changing every property that is supposed to be boolean or even adding partial classes that hold boolean properties connected to their string counterpart would be huge pain. Not to mention complications arising from regenerating code when we update from the database. (We plan to only make changes to the db directly and then run the update from database tool to update our models).

    I found this page outlining the database types that map to .NET types. Is there no way to customize any of these? We have a fairly large codebase with a lot of legacy code that actually uses string “True” and “False” values from the database.

    Perhaps it is not that simple and setting a mapping for varchar(5) to be created as a boolean property in the code that will get set properly from the database is not possible. If this is the case is there a way I can customize the code generation to implement one of your solutions above automatically?

    I did try to look into customizing code generation but I was somewhat overwhelmed  and if this is the route I have to take I could use some guidance.

    Any help you can offer is greatly appreciated.

    P.s. I should note we actually have telerik account where we have purchased a number of your products and have paid support included I believe. I just happened to post this question using my own account. Is there any benefit to linking our paid account to this question?
  5. Ady
    Admin
    Ady avatar
    589 posts

    Posted 22 May 2014 Link to this post

    Hi Sean,

     I have prepared a sample application where I have implemented a stringtoboolean converter. This converter is set for each varchar column that needs to be mapped to a 'bool' . This can be done by opening the 'Model Schema explorer' and specifying the Type fullname in the 'Converter' property.
    Once this is done all columns will be read/written via this converter.
    Hope this helps.

    If you link your account which has paid support you should receive a response within 24 hours which is not the case if support is not purchased. If active support is not purchased it could take upto 72 hours for a reply.

    Regards,
    Ady
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  6. Sean
    Sean avatar
    3 posts
    Member since:
    May 2014

    Posted 22 May 2014 in reply to Ady Link to this post

    Thank you, Ady. Except I think you way have forgotten to attach the application. I don't see a link anywhere... Perhaps I am missing something?
  7. Ady
    Admin
    Ady avatar
    589 posts

    Posted 22 May 2014 Link to this post

    Hello Seans,

    Oops..sorry about that.
    Here you go.

    Regards,
    Ady
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top
DevCraft banner