The Wayback Machine - https://web.archive.org/web/20110803115758/http://blog.abodit.com:80/category/programming/entity-framework-programming/

Entity Framework

The relationship could not be changed because one or more of the foreign-key properties is non-nullable

Today’s cryptic Entity Framework message …

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

The reason for this was a simple override to GetHashCode(). Entity Framework seems somewhat pedantic about how hash codes work.

Error 3032: Problem in mapping fragments : mapped to the same rows in table

Today I tried creating a two-level hierarchy using Entity Framework 4 but using the same discriminator column for both layers of the hierarchy.  It seems you cannot do this but instead must have a separate column for each layer of the hierarchy.  Makes some sense but the error message wasn’t very helpful.

I have a table-per-hierarchy model, one abstract class (Node), a column that differentiates the sub-classes (NodeType), and three sub-classes (NodeClass, NodeProperty, NodeIndividual) with conditions (NodeType=1, 2, 3), it’s working just fine.

I now want to add another sub-class which itself has three sub-classes.  So I create the new sub-class marking it abstract (NodeLiteral), I create the three derived types inheriting from it (NodeiteralInt, NodeLiteralDouble, NodeLiteralDateTime) and add conditions on them based on the same discriminator column used in the first level of inherited classes (NodeType=101,102,103).

When I compile I get:-

Error 3032: Problem in mapping fragments starting at lines … (classes) … are being mapped to the same rows in table Node. Mapping conditions can be used to distinguish the rows that these types are mapped to.

Since I DO have mapping conditions that are distinct for each of these classes the error message is somewhat confusing.

If I add another column and use that as the discriminator for the second level of inheritance it works.

So if you are trying to model a two or more layer hierarchy in EF don’t try to use a single column for all your conditions, create a column for each layer of the hierarchy instead.