What is easiest way to deal with converting 0/1 to False/True in EF 4.x?

三世轮回 提交于 2019-12-10 01:25:55

问题


The stored Proc returns a column with the value to be either 0 or 1 without converting to BIT. In my POCO, if I declare the field as

public bool MyColumn {get; set;}

I am getting this error:

The specified cast from a materialized 'System.Int32' type to the 'System.Boolean' type is not valid.

This actually makes sense since EF recognizes the returned value as an integer.

I am wondering that is there any easy way to (add annotation or use fluent api maybe) automatically convert 0/1 to False/True in the mapping behind the scene without touching the Proc?

Thanks in advance!


回答1:


What you can do is to have another Property to represent the Boolean representation . Decorate it with NotMapped attribute so that EF won't consider it for Mapping. Do and If condition and return true /false based on the value of Other property.

public Class Customer
{

  [NotMapped]
  public bool MyColumnBool 
  {
      get
      {
         return (MyColumn ==1);
      }
  }

  public int MyColumn {get; set;}
  // other properties

}



回答2:


Another option is to return a BIT from the stored procedure so you don't need to cast anything at C# side or use any weird decoration. This means, you can cast the integer value to BIT in T-SQL like I do below:

select col1, col2, CONVERT(BIT, CASE WHEN col3 IS NULL THEN 0 ELSE 1 END) as colWithBit
FROM table1



回答3:


In your ApplicationDbContext (the class that inherits from DbContext) you can use Fluent Api to convert values for the database.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  base.OnModelCreating(modelBuilder);
  modelBuilder.Entity<TheNameOfYourModelClass>()
    .Property(p => p.MyColumn)
    .HasConversion(
       v => v ? : 1 : 0,
       v => (v == 1) ? true : false);
}

Now, your database will contain 1 when inserting a true for MyColumn and vice-versa. When reading from your database 1 will be converted to true and vice-versa.




回答4:


Use System.Convert.ToBoolean(int)



来源:https://stackoverflow.com/questions/11783226/what-is-easiest-way-to-deal-with-converting-0-1-to-false-true-in-ef-4-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!