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

前端 未结 2 1639
醉话见心
醉话见心 2020-12-12 00:32

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 p

相关标签:
2条回答
  • 2020-12-12 00:52

    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"; }
    }
    
    0 讨论(0)
  • 2020-12-12 01:04

    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 :)

    0 讨论(0)
提交回复
热议问题