I have a problem updating a foreign key in an Entity Framework entity. I use self-controlled entities and have an entity with some relationships where the foreign key is also present as a property (one of the new EF4 functions). The key (integer) is marked as Nullable and Concurrency Mode fixed.
In particular, I have an Alarm object with a ratio of to many to 0..1 to the confirming user. (a user can acknowledge several alarms, but an alarm can only be acknowledged by zero or one user).
Entity definitions (simplified):
Alarm properties Id Int32 non-nullable identity entity key UserId Int32 nullable concurrency mode fixed Alarm navigation properties User 0..1 multiplicity User properties Id Int32 non-nullable identity entity key Name String non-nullable
In my self-tracking facility, the confirming user ID is automatically generated as Nullable in the same way as expected, however, if I assign a constant alarm to the user and run ApplyChanges, the self-control context extension tries to set the initial value (null) in the EF context (in SetValue to context extensions), but silently skips this because ClrEquivalentType for EdmType is invalid Int32.
Auto-generated extension code:
private static void SetValue(this OriginalValueRecord record, EdmProperty edmProperty, object value) { if (value == null) { Type entityClrType = ((PrimitiveType)edmProperty.TypeUsage.EdmType).ClrEquivalentType; if (entityClrType.IsValueType && !(entityClrType.IsGenericType && typeof(Nullable<>) == entityClrType.GetGenericTypeDefinition())) {
When EF later tries to update my alarm, I get an OptimisticConcurrencyException because it builds a WHERE clause in the UPDATE statement, where it uses 0 (zero) as the source value of the user's foreign key instead of the correct "null" value. (The WHERE clause is part of the optimistic Concurrency EF mechanism, where the initial values ββof properties marked as βfixedβ Concurrency are again checked by the properties in the database).
Are null foreign keys / primitive types not fully supported in self-monitoring objects for EF? If not, am I forced to use dummy objects instead of zero, or are there other workarounds?
Update I tried to reproduce the problem without STE, but a regular EF seems to cope with an upbeat Concurrency well for zero foreign keys, so this is a STE problem, not an EF problem. There are many problems with the objects of self-control, so it is not surprising that there is a glitch. If I find a workaround that can be implemented in the STE T4 script, I will post it here.