How to convert property type to EF 4.1 Code First

I want to create the first EF 4.1 Code models from an existing SQL database schema, and I wonder if it is possible to do some kind of transformation of these properties.

For example, I have an existing table "Foo" that has this field:

isTrue char(1) 'valid values are "Y" or "N"

In my first EF 4.1 Code model, I want to convert this field to a boolean type, for example:

public class Foo
{
    public bool isTrue { get; set; }  
}

Is this possible in EF 4.1 Code First by extending the DBContext or adding additional code to the model or EntityTypeConfiguration <> sub-class? If so, can someone point me to a link or some documentation on how to do this? Refactoring database fields is currently not possible.

+5
3

, , , , . , , ,

internal string YesNo { get; set; }

private bool _bYesNo;
[NotMapped]
public bool bYesNo
{
  get{return (YesNo == "Y") ? true : false;}
  set{_bYesNo = value;YesNo = (bYesNo) ? "Y" : "N";}
}
+4

EF.

, EF bool , true "Y" false "N".


, .

NHibernate, , , YesNo .

+2

I had the same problem and the following solution turned out great for me !!

Entity Framework and Oracle Boolean

In my code, the table field looks like a normal Boolean, but in the database it is char (1): the EF model converts it to “Y” or “N” and stores it in the database, I also do the same trick for Guid in char ( 36).

+1
source

All Articles