You can create a new property based on the existing property that is not mapped to the database and that performs the desired conversion. Assuming the column is named Foo
you can use this code:
[Column("Foo")]
public String FooWithWeirdNullValues { get; set; }
[NotMapped]
public String Foo {
get {
return FooWithWeirdNullValues == "NULL" || FooWithWeirdNullValues == "x"
? null : FooWithWeirdNullValues;
}
set { FooWithWeirdNullValues = value; }
}
In your code you then use the unmapped property to access the value of the Foo
column.
You might want to pick another name for FooWithWeirdNullValues
e.g. LegacyFoo
or whatever. You can pick any name as long as you use the Column
attribute to map it to the correct column in the database.
If desired you can make FooWithWeirdNullValues
private to not pollute your model. This requires a few changes to the DbContext
as described in an answer on Stack Overflow on how to map private properties using code first.