Using DB-Default values for unmapped bit columns in entity framework 4 (database first)
I am trying to map two concrete entity types and an abstract base type to the same database table.
The table contains a bit column that does not accept null. Column has a default value: ((0)).
Only one of the two concrete entity types (i.e. concrete type 1) needs to use the column's value (for the other (i.e. concrete type 2) it is always false).
I tried adding a property mapped to that column only to the entity type which requires it and a
When I call SaveChanges I get an UpdateException, with the following message on its inner-most-exception:
"The column cannot contain null values. [ Column name = MY_BIT_COLUMN,Table name = MY_TABLE ]"I already edited the SSDL section of the EDMX and changed:
<Property Name="MY_BIT_COLUMN" Type="bit" Nullable="false" />
to:
<Property Name="MY_BIT_COLUMN" Type="bit" Nullable="false" DefaultValue="false" />
(Without this change mapping failed - will not run)
Is there any way to get around this without adding a property mapped to this column to the second concrete entity type or moving it to the base type?
Adding property as protected to concrete type 2 does work, but I would prefer a more eleg开发者_运维技巧ant workaround.
If your workaround doesn't work (I'm little bit surprised but it is to late for me to test it now) then the only other workaround is changing your inheritance from TPH to TPT or TPC. The problem obviously is that TPH requires all columns in derived types to be nullable.
Other workaround is making your column member of parent entity and in derived entity use custom constructor which will always set it to false.
The last workaround is making your column nullable and enforce the validation in your business logic for the first entity.
精彩评论