Entity framework code first convert between class boolean and column integer

前端 未结 2 932
粉色の甜心
粉色の甜心 2021-01-13 02:56

I am using Entity Framework 5 code first. My table has a column called Active and its datatype is of type int. The values that are st

相关标签:
2条回答
  • 2021-01-13 03:08

    I tried the following because I do not know if Entity Framework can handle the conversion for me.

    I removed this line:

    this.Property(x => x.IsActive).HasColumnName("Active").HasColumnType("int");
    

    I then added a property to my CommandExecutionServer class:

    public class CommandExecutionServer : IEntity
    {
         public int Id { get; set; }
    
         public int? Active { get; set; }
    
         public bool IsActive
         {
              get
              {
                   return (Active == 1) ? true : false;
              }
         }
    }
    

    There might be a better way but this works for me for now. If any one can better this then please go ahead :)

    0 讨论(0)
  • 2021-01-13 03:21
     SELECT   CONVERT(A.bitcolumn as bit) as bitout
    

    ado.net will convert bits to bools. So, just convert your integer to a bit in your select statement in t-sql.

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