Note that there are some explanatory texts on larger screens.

plurals
  1. POAre nullable foreign keys allowed in Entity Framework 4?
    primarykey
    data
    text
    <p>I have a problem updating a foreign key in an Entity Framework entity. I am using self tracking entities and have an entity with some relations where the foreign key is also present as a property (one of the new features of EF4). The key (an integer) is marked as Nullable and Concurrency Mode fixed.</p> <p>Specifically I have an Alarm entity with a many to 0..1 relationship to a confirming user. (a user may confirm several alarms, but an alarm can be confirmed by only zero or one users).</p> <p>The entity definitions (simplified):</p> <pre><code>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 </code></pre> <p>In my self tracking entity the confirming user id is auto-generated as a Nullable just as expected, however if I assign a user to an already persistent alarm and run ApplyChanges, the self tracking context extension tries to set the original value (null) in the EF context (In SetValue in the context extensions), but silently skips that because the ClrEquivalentType of the EdmType is a non-nullable Int32.</p> <p>Auto-generated extension code:</p> <pre><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 &amp;&amp; !(entityClrType.IsGenericType &amp;&amp; typeof(Nullable&lt;&gt;) == entityClrType.GetGenericTypeDefinition())) { // Skip setting null original values on non-nullable CLR types because the ObjectStateEntry won't allow this return; } } int ordinal = record.GetOrdinal(edmProperty.Name); record.SetValue(ordinal, value); } </code></pre> <p>When the EF later tries to update my alarm I get an OptimisticConcurrencyException because it constructs a WHERE clause in the UPDATE statement where it uses 0 (zero) as the original user foreign key value instead of the correct "is null". (The WHERE clause is part of the EF optimistic concurrency mechanism, where the original values of the properties marked with "fixed" concurrency mode are checked agains the properties in the database).</p> <p>Are nullable foreign keys / primitive types not fully supported in self tracking entities for EF? If not, am I forced to use dummy entities instead of null or are there other workarounds?</p> <p><strong>Update</strong> I have tried to reproduce the problem without STE, but plain EF seems to handle optimistic concurrency well for nullable foreign keys, so this is an STE problem, not an EF problem. There is numerous issues with self tracking entities, so it is not surprising that there is a glitch here. If I find a workaround that can be implemented in the STE T4 script I will post it here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload