Entity Framework: tinyint to boolean mapping

c# entity-framework mapping


Entity Framework by default converts tinyint to bytes.

I attempted to convert the created type's underlying type to Boolean, but I received a compilation error.

The supplied Member Mapping is invalid. The member blah's type is "Edm.Boolean[Nullable=False,DefaultValue=]"

Can this be done in 4.0?

Using the tinyint column as a boolean wasn't my idea. Hibernate, which was used by another team to do this automatically, reportedly does so in order to maintain MySQL compatibility. Of course, tinyint has values other than 2. I'm trying to map it such that everything other than 1 is false and anything other than 0 is true. Any would be suitable for me.

Is it possible to include some form of type translator into EF?

10/25/2010 6:36:11 PM

Accepted Answer

According to MSDN's entry on numerical types, thetinyint type denotes an integer with a value between 0 and 255.

A bool , on the other hand, just depicts a binary.0 or 1 .

The default mapping was modified tobyte to bool (if that were even conceivable, which this website makes it sound like it's not) is absurd; how, for instance, would you express the value?42 a reliabletinyint ) as abool ?

If you need an entity with a type property,bool , It should be mapped to a column of type bit, in my opinion.

10/25/2010 5:21:52 PM

Popular Answer

In reality, the primary reason why integers are used so often in databases is because many database engines forbid indexes on bit fields. To save space, the majority of database engines aim to combine many bit fields into one "internal" byte. As a consequence, indexing cannot truly use the bit field.

The de facto rule is that all other values are true while zero is false. However, this kind of mapping is not supported by EF. The optimal approach is utilizing a private shadow field that is mapped to EF and declared as a byte. You then construct an alias boolean property that your code uses.

A little amount of reflection code is needed to map private properties with EF.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow